Lec 22 Pointers and Arrays
Lec 22 Pointers and Arrays
Lecture 22
main( ) Output:
{ Value of i = 3
int i = 3, *x ; Value of j = 1.500000
float j = 1.5, *y ; Value of k = c
char k = 'c', *z ; Original address in x = 65524
printf ( "\nValue of i = %d", i ) ; Original address in y = 65520
printf ( "\nValue of j = %f", j ) ; Original address in z = 65519
printf ( "\nValue of k = %c", k ) ; New address in x = 65526
x = &i ; New address in y = 65524
y = &j ; New address in z = 65520
z = &k ;
printf ( "\nOriginal address in x = %u", x ) ;
printf ( "\nOriginal address in y = %u", y ) ;
printf ( "\nOriginal address in z = %u", z ) ;
x++ ;
y++ ;
z++ ;
printf ( "\nNew address in x = %u", x ) ;
printf ( "\nNew address in y = %u", y ) ;
printf ( "\nNew address in z = %u", z ) ;
}
The way a pointer can be incremented, it can be decremented as well, to point to
earlier locations. Thus, the following operations can be performed on a pointer:
#include <stdio.h>
void main()
{
int arr[] = {10, 20, 36, 72, 45, 36};
int *j, *k;
j = &arr[4];
k = (arr + 4);
if (j == k)
printf (“The two pointers point to the same location”);
else
printf (“The two pointers do not point to the same location”);
}
•Do not attempt the following operations on pointers... they would never
work out.
–Addition of two pointers
–Multiplication of a pointer with a constant
–Division of a pointer with a constant
#include <stdio.h>
void display (int *, int);
void main()
{
int num[] = {24, 34, 12, 44, 56, 17};
display (&num[0], 6); // same as display (num, 6);
}
void display (int *j, int n)
{
int i;
for (i = 0; i <= (n-1); i++)
{
printf (“\nelement = %d”, *j);
j++; /* increment pointer to point to next element */
}
}
#include <stdio.h>
void main()
{
int num[] = {24, 34, 12, 44, 56, 17};
int i;
for (i = 0; i <= 5; i++)
{
printf (“\naddress = %u “, &num[i]);
printf (“element = %d %d “, num[i], *(num + i));
printf (“%d %d”, *(i + num), i[num]);
}
}
O/P:
address = 65512 element = 24 24 24 24
address = 65514 element = 34 34 34 34
address = 65516 element = 12 12 12 12
address = 65518 element = 44 44 44 44
address = 65520 element = 56 56 56 56
address = 65522 element = 17 17 17 17
Pointers and 2D arrays:
•Every dimension of an array is an array itself
•int s[5][2];
–Array of 5 elements, each of which is a one-dimensional array
containing 2 integers
#include <stdio.h>
void main()
{
int s[4][2] = {{1234, 56}{1212, 33}{1434, 80}{1312, 78}};
int i;
for (i = 0; i <= 3; i++)
printf (“\nAddress of %d th 1-D array = %u”, i, s[i]);
}
O/P:
Address of 0th 1-D array = 65508
Address of 1th 1-D array = 65512
Address of 2th 1-D array = 65516
Address of 3th 1-D array = 65520
2D arrays
•s is an array containing 4 one-dimensional arrays, each containing 2
integers
•Each one-dimensional array occupies 4 bytes (two bytes for each integer)
•These one-dimensional arrays are placed linearly (zeroth 1-D array s[0]
followed by first 1-D array s[1], etc.)
•Each one-dimensional array starts 4 bytes further along than the last one
2D arrays
•Which is the same as *(*(s+2)+1)
•To understand this concept consider the following fact.
–In case of one dimensional array s[2] would give us a value but in case of
two dimensional array s[2] would give us an address since in case of two
dimensional array s[2] does not contain a value but another 1 dimensional
array.
/* Pointer notation to access 2-D array elements */
#include <stdio.h>
void main()
{
int s[4][2] = {{1234, 56}, {1212, 33}, {1434, 80}, {1312, 78}};
int i, j;
for (i = 0; i <= 3; i++){
printf (“\n”);
for (j = 0; j <= 1; j++)
printf (“%d”, *(*(s + i)+j)); or printf (“%d”, *(s[i]+j));
}
}
O/P:
1234 56
1212 33
1434 80
1312 78
Pointer to an Array
If we can have a pointer to an integer, a pointer to a float, a pointer to a char, then
can we not have a pointer to an array? We certainly can. The following program
shows how to build and use it.
/* Usage of pointer to an array */
#include <stdio.h>
void main()
{
int s[4][2] = {{1234, 56}{1212, 33}{1434, 80}{1312, 78}};
int (*p)[2];
int i, j, *pint;
for (i = 0; i <= 3; i++)
{
p = &s[i]; And here is the output...
pint = (int *) p; 1234 56
printf (“\n”); 1212 33
for (j = 0; j <= 1; j++) 1434 80
printf (“%d”, *(pint + j)); 1312 78
}
}
Here p is a pointer to an array of two integers. Note that the
parentheses in the declaration of p are necessary. Absence of
them would make p an array of 2 integer pointers. In the outer
for loop each time we store the address of a new one-
dimensional array. Thus first time through this loop p would
contain the address of the zeroth 1-D array. This address is then
assigned to an integer pointer pint. Lastly, in the inner for loop
using the pointer pint we have printed the individual elements
of the 1-D array to which p is pointing.
void main( ) show ( int ( *q )[4], int row, int col ) // 2nd way
{ {
int a[3][4] = { int i, j ;
1, 2, 3, 4, int *p ;
5, 6, 7, 8, for ( i = 0 ; i < row ; i++ )
9, 0, 1, 6 {
}; p=q+i;
display ( a, 3, 4 ) ; for ( j = 0 ; j < col ; j++ )
show ( a, 3, 4 ) ; printf ( "%d ", * ( p + j ) ) ; }}
print ( a, 3, 4 ) ;
} print ( int q[ ][4], int row, int col ) // 3rd way
display ( int *q, int row, int col ) // one {
way int i, j ;
{ for ( i = 0 ; i < row ; i++ )
int i, j ; {
for ( i = 0 ; i < row ; i++ ) for ( j = 0 ; j < col ; j++ )
{ printf ( "%d ", q[i][j] ) ; }}
for ( j = 0 ; j < col ; j++ )
printf ( "%d ", * ( q + i * col + j ) ) ; }}
In the display( ) function we have collected the base address of the 2-D array being
passed to it in an ordinary int pointer. Then through the two for loops using the
expression * ( q + i * col + j ) we have reached the appropriate element in the array.
Suppose i is equal to 2 and j is equal to 3, then we wish to reach the element a[2][3]. Let
us see whether the expression * ( q + i * col + j ) does give this element or not. Refer
Figure below to understand this.