Chapter 4 - Pointers2
Chapter 4 - Pointers2
int t[10]
t is equivalent to &t[0]
t is a constant pointer to integers
The following notations are the same
t+1 same as &t[1]
t+i same as &t[i]
t[i] same as *(t+i)
These are two ways to store the value 1 to each of 10 elements of the array t
using pointers
int i ; int i ;
for (i=0;i<10;i++) int *p; //variable pointer
*(t+i) = 1 ; for (p = t, i=0 ; i<10 ; i++, p++)
*p = 1 ;
Remark: we cannot do t++
because t is a constant pointer cout<<p[2]; //correct 1
Example
cout<< A; →&A[0]
cout<< A + 1; →&A[1]
cout<< *A + 1; →6 = A[0] + 1
cout<<*(A+i) →A[i]
2
Exp
int *p;
Method3 for(i=0, p=A; i<5; i++,p++)
cout << *p;
int *p;
Method4 for(p=A; p< A+5; p++)
cout << *p; 3
TYU
Write a function that asks the user to input an array of n integers
The array is by default passed by address. The name of the array is a pointer to the
1st element = The name is the address of the 1st element
void inputArray (int A[], int n)
{
int i;
for(i=0;i<n;i++)
cin>>A[i]; //cin >> *(A+i);
}
int t[3][4] ;
t is an array with 3 « elements »,
▪ each of these elements is itself an array of 4 integers
5
Arrays with many indices (cont.)
t[3][4]
Adr Val
1 2 3 4
t 3000 1 &t[0][0] or &t[0]
5 6 7 8
3004 2
9 10 11 12
3008 3
3012 4
Remarks t+1 3016 5 &t[1][0] or &t[1]
• t[i][j] = *(*(t+i)+j)
3020 6
• t[1] is a constant pointer 3024 7
• not lvalue 3028 8
• t[1]++ → Error
t+2 3032 9 &t[2][0] or &t[2]
3036 10
3040 11
3044 12
6
Null Pointer
A null pointer
▪ points on nothing
▪ has a value 0
▪ is a constant pointer
It is very important to insure that the pointer points somewhere known before
using it (or it is null)
7
Dynamic allocation: operators new and delete
8
Internal structure of a process
9
Syntax and role of new
x
Syntax of new:
▪ Variable ?
▪ type *var = new type;
- int *x;
12
- x = new int; int *x = new int;
- *x = 12;
▪ Allocate the necessary memory for one element of type int, and
▪ Assign to x the corresponding address
▪ 1D Array
▪ type *var = new type[n];
- n can be a variable or a constant A
int n;
cin>>n;
? ? … ?
int * A = new int [n];
▪ Allocate the necessary memory for an array of n integers, and
▪ Put the starting address in A
10
Syntax and role of new (cont.)
▪ 2D Array n x m
type **var = new type *[n];
for(int i=0; i<n; i++)
var[i] = new type[m];
▪ Exp:
int **a=new int*[n];
for(int i=0;i<n;i++)
a[i]=new int[m];
Remarks
▪ Parenthesis in declaration are important
- int(*ptab)[4]; //a pointer on an array of 4 int
- int *ptr[3]; // an array of pointers to integers
11
Operator delete
Example
delete x ; //in case of simple variable
delete A or delete [] A; //in case of an array
12
Example illustrating the manipulation of dynamic allocation memory
#include <iostream>
using namespace std ;
int main()
{ int *adi, *adibis ; Screen:
int nb ; How many values: 7
float * adf ; allocation of 7 int in : 8861976
These are the squares of numbers 1 to 7 :
cout << "How many values : " ; 1 4 9 16 25 36 49
cin >> nb ;
delete adi ;
cout << "Deallocation of "<<nb<<" int in : "<< adi << "\n";
//here it is dangerous to use the spaces pointed by adibis, and also those pointed
by adi
adi = new int [50] ;
cout << "Allocation of 50 int in: " << adi << "\n" ;
delete adf ;
cout << "Deallocation of 30 float in: " << adf <<"\n" ;
15
Passing arrays as argument: Array with multiple indices
Function definition
Function call
functionName(arrayName, nbUsedRows, nbUsedColumns);
16
Passing arrays as argument: Array with multiple indices
Example
int main()
{ int A[3][4];
.....
initialize (A, 3, 4);
.....
}
17
Array with multiple indices
for(int i=0;i<n;i++)
delete a[i];
return 0;
}
18