CS-107
COMPUTER
PROGRAMMING
MEHREEN TAHIR
Pointers and Arrays
LECTURE-9
Pointers
• Variables
– Value
– Location
• Pointers
– Auxiliary Variables
– Store Location
Pointers
Pointers
• Dynamic Memory
– Memory needs of a program can only be
determined during runtime
– Programs need to dynamically allocate memory,
for which the C++ language integrates the
operators new and delete.
Pointers p 8
q 9
• Dangling pointer
int *p=new int; p 8
int *q=new int; q
9
*p=8;
*q=9; p
p=q; q
delete q; // dangling pointer p
q=NULL; q
NULL
Pointers
• Call by reference
void func(int * py){
++(*py);
cout<<*py;
} //end func
void main(){
int x=5;
cout<< x;
func(&x);
}
Pointers
• Call by reference
void func(int * py){
++(*py);
cout<<*py;
} //end func
void main(){
int *y;
int x=5;
y=&x;
cout<< x << *y;
func(y);
}
Pointers
• Call by reference
void func(int * py){
++(*py);
cout<<*py;
} //end func
void main(){
int x=new int;
*x=5;
cout<< *x;
func(x);
}
Pointers
ARRAYS
• ARRAYS
– A 1-D array is a finite, ordered set of
homogeneous elements
– E.g. int a[100] specifies an array of 100 integers
– Two basic operations:
• Extraction e.g. a[i]
• Storage e.g. a[i]=x;
– No. of elements in an array=upper bound – lower
bound + 1
Example: Using 1-D Arrays
void main()
{
int arr[5]={4,3,6,7,1};
int sum=0;
for (int i=0; i<5; i++) {
sum=sum+arr[i];
}
cout<<"Sum="<<sum<<endl;
}
Implementing 1-D Array
• By implementation of a data type we mean what the
hardware or system software (e.g. compiler) does to
support that data type.
• The declaration int b[100] reserves 100 successive
memory locations, each large enough to contain a
single integer.
• The address of the first location is the base address of
array b, and let us denote it as base(b)
• Suppose size of each individual element is esize
• Then b[0] is at base(b), b[1] is at base(b)+1*esize, b[2]
is at base(b)+2*esize, and element i is at base(b)
+i*esize
Example: 1-D Array Element Access
• Address of A[i]=base(A)+i*esize
• Here base(A)=100 and esize=4
100 A[0]
104 A[1]
108 A[2]
112 A[3]
116 A[4]
120 A[5]
Addresses of Individual Elements in int A[6]
Implementing 1-D Array
• An array variable is implemented as a pointer
variable.
• E.g. in int b[100], the type of b is “pointer to
int” or “int *”.
Arrays as Parameters
• Arrays are passed by reference (WHY?)
float avg(float b[], int size) /*no range is specified for the
array b */
{
float sum;
sum=0;
for (int i=0; i<size; i++)
sum+=b[i];
return (sum/size);
}
Arrays as Parameters
void main()
{
float arr[5]={4.1,3.6,6.2,7.3,1.9};
cout<<avg(arr,5)<<endl;
}
Arrays as Parameters
1-D Array
arr arr is the pointer to the first location.1000
1000 represents address of first location
*arr *(arr+1) *(arr+2) *(arr+3) *(arr+4)
arr[0] arr[1] arr[2] arr[3] arr[4]
4.1 3.6 6.2 7.3 1.9
1000 1004 1008 1012 1016
arr (arr+1) (arr+2) (arr+3) (arr+4)
Dynamic Array
• 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=5.
• Allocate an array using the new operator
• We must free the memory we got using the new
operator once we are done with that array.
delete[ ] arr;
Dynamic Array
int size=5;
int input=0;
int* arr=new int[size];
for(int i=0;i<size;i++)
{
cin>>input;
arr[i]=input;
}
for(int i=0;i<size;i++)
{
cout<<arr[i]<<endl;
}
delete [] arr;
arr=NULL;
2-D Arrays
• Rows as well as columns e.g. int a[3][5]
• The number of rows or columns is called
range of that dimension
• A 2-D array only a logical data structure,
because physical hardware does not have
such facility (i.e. memory is linear)
2-D Arrays
• Thinking in 2 dimensions is convenient to
programmers in many situations, e.g.
– A map
– A checkerboard
– Any set of values that are dependent on two
inputs; a departmental store that has 20 branches
each selling 30 items
int sales[20][30];
Implementing a 2-D array
• Mapping from 2-D logical space to 1-D
physical space
• Two approaches
– Row major order
– Column major order
Row-Major Order
• The first row of the array occupies the first set
of memory locations reserved for the array,
the second row occupies the second set, and
so on.
A[0][0]
A[0][1]
Row 0
A[0][2]
A[1][0]
Row 1 A[1][1]
A[1][2]
Representation of A[2][3] in Row-major Order
Example: Element Access in Row-
Major Order
• Address of A[i1][i2]=base(A)+(i1*r2+i2)*esize
• Here base(A)=100, r1=2, r2=3, esize=4
100 A[0][0]
104 A[0][1]
Row 0
108 A[0][2]
112 A[1][0]
Row 1 116 A[1][1]
120 A[1][2]
Representation of A[2][3] in Row-major Order