DSA Lecture02 Basics
DSA Lecture02 Basics
• Pointers
• References
• Arrays
Storing address in pointers
2
Passing a pointer variable to a function (cont.)
#include <iostream>
void newval(float *, float *); // function prototype
int main() {
3
Reference variables (cont.)
• Very useful for calling a function by reference.
4
Differences between references and
pointers
a) A reference parameter is a constant pointer (after
initializing a reference parameter, we cannot change it
again).
5
Differences between references and pointers
(cont.)
6
Array names as pointers (cont.)
#include <iostream>
void main() {
const SIZE = 5
7
Dynamic Array Allocation
• To avoid wasting memory, array allocation or
deallocation can take place at run time
• To allocate memory, we need to use the new operator
Reserves the number of bytes requested by the declaration.
Returns the address of the first reserved location or NULL if
sufficient memory is not available.
• To deallocate memory (which has previously been
allocated using the new operator) we need to use the
delete operator.
Releases a block of bytes previously reserved. The address of
the first reserved location is passed as an argument to
delete.
8
Dynamic Arrays
You would like to use an array data structure
but you do not know the size of the array at
compile time.
You find out when the program executes that
you need an integer array of size n=20.
Allocate an array using the new operator:
y = &x[0];
y = x; // x can appear on the right
// y gets the address of the
// first cell of the x array
Intructor: Sadia Arshid, DCS&SE 13
Dynamic Arrays
‘y’ is a lvalue; it is a pointer that holds the
address of 20 consecutive cells in memory.
It can be assigned a value. The new operator
returns as address that is stored in y.
We can write:
y = &x[0];
y = x; // x can appear on the right
// y gets the address of the
// first cell of the x array
Intructor: Sadia Arshid, DCS&SE 14
Dynamic Arrays
‘y’ is a lvalue; it is a pointer that holds the
address of 20 consecutive cells in memory.
It can be assigned a value. The new operator
returns as address that is stored in y.
We can write:
y = &x[0];
y = x; // x can appear on the right
// y gets the address of the
// first cell of the x array
Intructor: Sadia Arshid, DCS&SE 15
Dynamic Arrays
We must free the memory we got using the
new operator once we are done with the y
array.
delete[ ] y;
int *arr;
arr = new int[SIZE]; // allocation
17
case of 2D arrays
cout << "Enter numRows and numCols: ";
cin >> numRows >> numCols;
int **arr2D;
arr2D = new int* [numRows]; // allocation
delete [] arr2D;
19
Structures
• A struct holds data, like an array
• Each unit of data in a struct is called a data member (or
member)
• In a struct, each data member can have a different data
type
– in arrays, the data type of each element is the same
20
Example of declaring a struct
• /* declare a struct type (template) with name "Person_Record"*/
struct Person_Record {
char name[20];
int age;
};
/* Now, allocate memory */
struct Person_Record My_Record; /* a single record */
struct Person_Record PR_Array[100]; /* an array of records */
21
I/O of Structures
• When you use cin/cout you must read/write a
structure field-by-field.
• Emp.age = 20
• Cin >> emp.age
• Cout << emp.age
22
#include <iostream.h>
#include <conio.h> do {
cout << "Please enter Name of the
#include <stdio.h> employee" << endl;
struct Date{ cin >> d[i].name ;
int mm;
int dd; cout << "Please enter age" << endl;
cin >> d[i].age ;
int yy; cout << "Please enter date
}; (dd,mm,yyyy)" << endl;
struct data{ cin >> d[i].hireDate.dd;
char name[25]; cin >> d[i].hireDate.mm;
cin >> d[i].hireDate.yy;
int age; cout << "Would like to enter more data?
struct Date hireDate; (y/n)" << endl;
}; i++;
cin >> ch;
void main(void) }
{ while (ch != 'n');
data d[20]; for (int j=0; j < i; j++)
//creates array of type data with {
cout << endl << d[j].name;
limit 20 }
int i = 0; }
char ch;
23