Professional Documents
Culture Documents
Data Structure - Chap 3
Data Structure - Chap 3
Mr Zahi Al Chami
Chap 3: Pointers and Structures
Introduction
The variable that stores the address of another variable
(like foo in the previous example) is what in C++ is called
a pointer.
… … 100 … 1024 …
integer
pointer
Declare a pointer
To declare a pointer, you should define its type and its
name as well.
For example:
1020 1024
… … 100 … … …
int a = 100;
cout << a; //prints 100
cout << &a; //prints 1024
The pointer variable
1020 1024 1032
… 88 100 … 1024 …
int a = 100;
int *p = &a; Result is:
cout << a << " " << &a <<endl; 100 1024
cout << p << " " << &p <<endl; 1024 1032
… 88 100 … 1024 …
int a = 100; Result is:
int *p = &a; 100
cout << a << endl; 1024
cout << &a << endl; 1024 100
cout << p << " " << *p << endl; 1032
cout << &p << endl;
Increment a pointer
int x, y, *p
x=4
p=&x // p contains address of x
y=*p // y contains the content value of address p
(*p)++ // increment the content of address p by
1, which means increment x by 1
int *p;
p = 0;
cout << p << endl; //prints 0
cout << &p << endl;//prints address of p
cout << *p << endl;//Error!
Static allocation vs dynamic allocation
Static allocation is done when the program is initialized,
before its actual execution.
For example:
int* p = new int;
Uninitialized int variable
p
Operator New: Example
Operator Delete
Free the allocated memory.
Exemple:
Delete ad ;
Delete adc;
20
Operator Delete: Example
21
Pointers and Arrays
The name of an array designates only the first element and
not the entire array.
22
Example
#include <iostream>
using namespace std;
Result:
Address of a[0]: 0x0065FDE4
Name as pointer: 0x0065FDE4
23
Dereferencing an array
#include <iostream>
a[0] 2 using namespace std;
a void main(){
a[1] 4
int a[5] = {2,4,6,8,22};
a[2] 6 cout << *a << " "
<< a[0];
a[3] 8 } //main
a[4] 22
24
Dereferencing an array
#include <iostream>
a p using namespace std;
void main(){
a[0] 2 int a[5] = {2,4,6,8,22};
a[1] 4 int *p = a;
6 cout << a[0] << " "
a[2]
<< *p;
a[3] 8 }
a[4] 22
25 a
25
Dereferencing an array
a[0] or *(a + 0) 2 a
a[1] or *(a + 1) 4 a + 1
a[2] or *(a + 2) 6 a + 2
a[3] or *(a + 3) 8 a + 3
a[4] or *(a + 4) 22 a + 4
27
Double pointers 2D arrays
To dynamically create a 2D array, we write:
int **B=new int *[n] // n is the number of rows
for(int i=0;i<n;i++)
B[i]=new int[m]; // each pointer points to an array and m is the
number of columns.
28
Exercises
Write a procedure that takes as inputs two integers and swaps
their values using pointers.
30
Introduction
What is a variable ?
it is a cell of the memory having a type and a value.
31
Structure declaration
32
Accessing the fields of a structure
The fields are handled like any other variable.
Example:
item1.number = 15 ;
cout << item1.number ;
cin >> item2.price ;
item1.number++;
33
Structures including other structures
34
Passing a structure by value
35
Passing a structure by reference
36
Passing a structure as a pointer
37
A function returning a structure
38
Array of structures
Let us consider the following structure:
40
Exercises
Exercise 1:
using the below structure:
struct point{
double x;
double y;
};
41
Exercises
Exercise 2:
a structure student is defined as follows:
struct student{
char fname [20];
char lname [20];
int age;
char adresse [20];
double GPA;
};
42
Exercises
Exercise 3:
struct item{
char name [20];
char description [20];
double price;
int quantity;
};
43
Exercises
Trace the algorithm below to find the result