Chapter 4 - Pointers1
Chapter 4 - Pointers1
Chapter 4 – Pointers
1
Data types in C++
pointer reference
enum
2
Pointer
▪ Object whose value represents the location of another object
3
Pointers variables
Declaration: DataType* var;
Multiple declarations: DataType *var1, *var2, …;
Examples
int * ad ;
int n ;
n = 20 ;
ad = &n ; //assign the address of n to ad
*ad = 30 ;// assign *ad the value 30, n becomes 30
The variable ad
▪ is a pointer to integers, pointer of type int*
▪ Contains the address of an integer
Representation of pointers
Address Variable
ad n 5000 5008 ad
20
30 5008 30 n
5
Motivation
6
Some examples
7
Some examples (cont.)
Remarks:
1. ad et *ad are lvalues, but &ad is not
2. int * ad ; reserves a memory place for a pointer pointing to an integer, but it does not
reserve the integer.
4. int x =5;
&x=10; //error
5. int x=10;
int y = &x; //y will contain the integer value of address of x
int * ad ;
ad++;
ad contains the address of the next integer (element) (i.e. the integer that is
after *ad)
In fact, the address is increased by sizeof(dataType) bytes.
In this case, it’s sizeof(int)
9
Reference type
int& j = i;
▪ j is a reference (of type reference) to an integer (type int&) which is now equivalent to i.
10
Reference type
Remarks:
1. int &j = i; //same as: int& j = i;
▪ &j, means that the memory place referenced by j, is of type integer, and is the same as i
11
Example
Suppose:
Adr x = 3000 int x = 5;
Adr y = 4000 int &y = x;
Adr p = 2000 int *p;
int *p = &x; Address Variable
p = &x;
cout << *p; → 5 2000 3000 p
cout << p; → 3000 (&x)
cout << &p; → 2000
cout << x; → 5 3000 67
8
5 x
cout << &x; → 3000
cout << y; → 5
cout << x++; → 5 //postincrementation 85
67
4000 y
cout << *p; → 6
cout << y; → 6
cout << ++y; → 7
cout << x; → 7
*p++;
cout << x; → 8
12
Pointer on a pointer - Example
Suppose
&n = 0x22ff40 Address Variable
&pn = 0x22ff44
&ppn = 0x22ff5c 0x22ff40 44 n
int n = 44;
cout << "n = " << n << endl; → n = 44
cout << "&n = " << &n << endl; → &n = 0x22ff40 0x22ff44 0x22ff40 pn
int* pn = &n;
cout << "pn = " << pn << endl; → pn = 0x22ff40
0x22ff5c 0x22ff44 ppn
cout << "&pn = " << &pn << endl; → &pn = 0x22ff44
cout << "*pn = " << *pn << endl; → *pn = 44
13
Passing argument to a function
14
Passing arguments by value
#include <iostream.h>
Address Variable
void swap (int, int );
3000 10 n
int main(){
int n = 10, p = 20 ;
cout << "before call: " << n << " " << p << "\n"; 3004 20
5 p
swap (n, p) ;
cout << "after call: " << n << " " << p << "\n";
return 0;
}
Address Variable
void swap (int a, int b){
int tmp ; 5000 10
20 a
cout << "start swap: " << a << " " << b << "\n";
tmp = a ;
a = b ;
5004 10
20 b
b = tmp ;
cout << "end swap: " << a << " " << b << "\n"; 5008 10 tmp
}
before call: 10 20
Output start swap: 10 20
end swap: 20 10
15
after call: 10 20
Passing arguments by reference
#include <iostream.h>
Address Variable
void swap (int&, int& );
3000 20
10 n a
int main(){
int n = 10, p = 20 ;
cout << "before call: " << n << " " << p << "\n"; 3004 10
20 p b
swap (n, p); // attention, here there is no &n, &p
cout << "after call: " << n << " " << p << "\n";
return 0;
} int& a = n;
int& b = p; Address Variable
void swap (int& a, int& b){
int tmp ;
cout << "start swap: " << a << " " << b << "\n"; 5008 10 tmp
tmp = a ;
a = b ;
b = tmp ;
cout << "end swap: " << a << " " << b << "\n";
}
before call: 10 20
Output start swap: 10 20
end swap: 20 10
16
after call: 20 10
Passing arguments by address with pointers
#include <iostream.h>
Address Variable
void swap (int*, int* );
3000 20
10 n
int main(){
int n = 10, p = 20 ;
cout << "before call: " << n << " " << p << "\n"; 3004 10
20 p
swap (&n, &p);// attention, here we have &n, &p
cout << "after call: " << n << " " << p << "\n";
return 0;
} int* a = &n;
int* b = &p; Address Variable
void swap (int* a, int* b){
int tmp ; 5000 3000 a
cout << "start swap: " << *a << " " <<* b << "\n";
tmp = *a ;
*a = *b ;
5004 3004 b
*b = tmp ;
cout << "end swap: " << *a << " " << *b << "\n"; 5008 10 tmp
}
before call: 10 20
Output start swap: 10 20
end swap: 20 10
17
after call: 20 10
Remark
18
int * a; // a is a pointer to integer
int x = 10;
a = &x;
*a = 20;
19
TYU
Write a function that asks the user to input an integer value
20