0% found this document useful (0 votes)
4 views21 pages

Lec 22 Pointers and Arrays

Download as pptx, pdf, or txt
0% found this document useful (0 votes)
4 views21 pages

Lec 22 Pointers and Arrays

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 21

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:

Addition of a number to a pointer.


For example,
int i = 4, *j, *k ;
j = &i ;
j=j+1;
j=j+9;
k=j+3;

Subtraction of a number from a pointer.


For example,
int i = 4, *j, *k;
j = &i;
j = j -2;
j -= 5;
k = j – 6;
Subtraction of one pointer from another
•One pointer variable can be subtracted from another provided they point to
elements of the same array
–Result is the number of values separating the two
#include <stdio.h>
void main()
{
int arr[] = {10, 20, 30, 45, 67, 56, 74}; // array begins at 65502
int *i, *j;
i = &arr[1];
j = &arr[5];
printf (“%d %d”, j – i, *j - *i); // j – i = 4, *j - *i = 36
}
Comparison of two pointer variables:

#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

•Two important points:


–Array elements are always stored in contiguous memory locations
–A pointer when incremented always points to an immediately next
location of its type
#include <stdio.h>
void main()
{
int num[] = {24, 34, 12, 44, 56, 17};
int i, *j;
j = &num[0];
for (i = 0; i <= 5; i++)
{
printf (“\naddress = %u “, j);
printf (“element = %d”, *j);
j++;
}
}
Passing arrays between functions:

#include <stdio.h> #include <stdio.h>


void display (int); void display (int *);
void main() void main()
{ {
int i; int i;
int marks[] = {55, 65, 75, 56, 78, 78, 90}; int marks[] = {55, 65, 75, 56, 78, 78, 90};
for (i = 0; i <= 6; i++) for (i = 0; i <= 6; i++)
display (marks[i]); display (&marks[i]);
} }
void display (int m) void display (int *m)
{ {
printf (“%d ”, m); printf (“%d ”, *m);
} }
O/P: 55 65 75 56 78 78 90 O/P: 55 65 75 56 78 78 90
Passing an Entire Array to a Function:

#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.

But why should we use a pointer to an array to print elements


of a 2-D array. Is there any situation where we can appreciate
its usage better? The entity pointer to an array is immensely
useful when we need to pass a 2-D array to a function. This is
discussed in the next slides.
Passing 2-D Array to a Function
/* Three ways of accessing a 2-D array */

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.

The expression * ( q + i * col + j ) becomes * ( 65502 + 2 * 4 + 3). This turns out to


be * (65502 + 11 ). Since 65502 is address of an integer, * ( 65502 + 11 ) turns out to
be * (65524). Value at this address is 6. This is indeed same as a[2][3]. A more
general formula for accessing each array element would be:
* ( base address + row no. * no. of columns + column no. )
In the show( ) function we have defined q to be a pointer to an array of 4
integers through the declaration:
int ( *q )[4] ;
To begin with, q holds the base address of the zeroth 1-D array, i.e. 4001
(refer Figure in previous slide). This address is then assigned to p, an int
pointer, and then using this pointer all elements of the zeroth 1-D array are
accessed. Next time through the loop when i takes a value 1, the
expression q + i fetches the address of the first 1-D array. This is because,
q is a pointer to zeroth 1-D array and adding 1 to it would give us the
address of the next 1-D array. This address is once again assigned to p, and
using it all elements of the next 1-D array are accessed.
In the third function print( ), the declaration of q looks like this:
int q[ ][4] ;
This is same as int ( *q )[4], where q is pointer to an array of 4 integers.
The only advantage is that we can now use the more familiar expression
q[i][j] to access array elements. We could have used the same expression
in show( ) as well.

You might also like