0% found this document useful (0 votes)
10 views

DSA Lecture02 Basics

Uploaded by

Engr Sameer Hani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

DSA Lecture02 Basics

Uploaded by

Engr Sameer Hani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Basics

• Pointers
• References
• Arrays
Storing address in pointers

int num = 22;


int * num_addr = #

2
Passing a pointer variable to a function (cont.)
#include <iostream>
void newval(float *, float *); // function prototype
int main() {

float firstnum, secnum;


cout << "Enter two numbers: ";
cin >> firstnum >> secnum;
newval(&firstnum, &secnum); // pass the address explicitly !!
cout << firstnum << secnum << endl;
return 0;
}

void newval(float *xnum, float *ynum) {


*xnum = 89.5; // dereferencing is required !!
*ynum = 99.5;
}

3
Reference variables (cont.)
• Very useful for calling a function by reference.

void newval(float& xnum, float& ynum) {


xnum = 89.5;
ynum = 99.5;
}

4
Differences between references and
pointers
a) A reference parameter is a constant pointer (after
initializing a reference parameter, we cannot change it
again).

b) References are dereferenced automatically (no need


to use the dereferencing operator *).

5
Differences between references and pointers
(cont.)

int b; // using reference variables


int& a = b;
a = 10;

int b; // using pointers


int *a = &b;
*a = 10;

6
Array names as pointers (cont.)

#include <iostream>

void main() {
const SIZE = 5

int i, arr[SIZE] = {98, 87, 92, 79, 85};

for(i=0; i<SIZE; i++)


cout << arr[i] << *(arr + i) << endl;

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:

int* y = new int[20]; // or int* y = new int[n]


y[0] = 10;
y[1] = 15; // use is the same
Intructor: Sadia Arshid, DCS&SE 10
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:

int* y = new int[20]; // or int* y = new int[n]


y[0] = 10;
y[1] = 15; // use is the same
Intructor: Sadia Arshid, DCS&SE 11
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:

int* y = new int[20]; // or int* y = new int[n]


y[0] = 10;
y[1] = 15; // use is the same
Intructor: Sadia Arshid, DCS&SE 12
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 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;

 We would not do this to the x array because we


did not use new to create it.

Intructor: Sadia Arshid, DCS&SE 16


case of 1D arrays
cout << "Enter array size: ";
cin >> SIZE;

int *arr;
arr = new int[SIZE]; // allocation

delete [] arr; // deallocation

17
case of 2D arrays
cout << "Enter numRows and numCols: ";
cin >> numRows >> numCols;

int **arr2D;
arr2D = new int* [numRows]; // allocation

for(i=0; i<numRows; i++)


arr2D[i] = new int[numCols];

for(i=0; i<numRows; i++) // deallocation


delete [] arr2D[i];

delete [] arr2D;

(individual elements can be accessed using


indices!! (e.g., arr2D[0][2]=10;))

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

You might also like