Next: Dynamic memory Allocation Up: Main Previous:Pointers

Some examples of Arrays and pointers

In the last lecture we saw about pointers and arrays, pointer to a pointer and pointer to arrays. We will summarize some of these concepts through the example below.

 

Pointer-Summary-1.c

#include <stdio.h>

#include <math.h>

main()

{

            int num [ ] = { 10,20,30,40,50 }

            print ( &num, 5, num);

}

print ( int *j, int n, int b[5])

{

            int i;

            for(i=0;i<=4;i++)

            {

                        printf (  "  %u  %d  %d  %u  \n  ", &j[i]  ,  *j , *(b+i)  ,  &b);

                        j++;

            }

}

 

In this example we have a single dimensional array num and a function print . We are passing,  the address to the first element of the array, the number of elements and the array itself, to this function.  When the function receives this arguments, it maps the first one to another pointer j and the array num  is copied into another array b .  (The type declarations are made here itself. Note that these declarations can also be given just below this line). j is now a pointer to the array b.

 Inside the function we are printing out the address of the array element and the value of the array element in two ways. One using the pointer j and the other using the array b. If we compile and run this code we get the following out put,

3221223408 10 10 3221223376
3221223416 20 20 3221223376
3221223424 30 30 3221223376
3221223432 40 40 3221223376
3221223440 50 50 3221223376

  

Note that as we increment j it points to the successive elements of the array. We can get both the address of the array elements and the value stored there using this. However the array name, which acts also as the pointer to its base address is not able to give us the address of its elements. Or in other words, the array name is a constant pointer. Also note that while j  is points to the elements of the array num, b is pointing to its copy.

 

Next we have an example that uses a  two dimensional array. Here care should be taken to declare the number of columns  correctly.

 

Pointer-summary-2.c

 

#include <stdio.h>

#include <math.h>

main()

{

            int arr [ ][3] = {{11,12,13}, {21,22,23},{31,32,33},{41,42,43},{51,52,53}};

            int I , j ;

int  *p ,  (*q) [3], *r ;

p = (int *) arr ;

q = arr;

r = (int *) q ;

printf (  "  %u  %u  %d  %d  %d  %d \n  ", p ,  q  ,  *p , *(r)  ,  *(r+1),  *(r+2));

p++ ;

q++ ;

r = (int *) q ;

printf (  "  %u  %u  %d  %d  %d  %d \n  ", p ,  q  ,  *p , *(r)  ,  *(r+1),  *(r+2));

 

 

}

 

Here we have a pointer p and a pointer array q. The first assignment statement is to make the  pointer p points to the array arr.  While assigning, we also declare the type of the variable arr.  Note that variables on both side of this statement should have the same type.  Next line is a similar statement , now with q and arr . Since q is a pointer array, the array can be directly assigned to it and there is no need for specifying the type of the variable.  In the next line we make the pointer r to point to the pointer array q . Then we will print out the different values. Here is what we get from this , 

3221223344  3221223344  11 11 12 13

3221223348  3221223356  12 21 22 23

 

Here we see that incrementing  p  make it just jump through each element of the array, where as  incrementing q,  will move it from one row to another row.

Passing 2 Dimensional Arrays to a Function

            So far we have looked at the ways to pass a one dimensional array to a function. Let us now extend this to the case of a two dimensional array. There are four different ways of passing a two dimensional array to a function

 We will illustrate the use of these four methods through the sample program. In this we pass a two dimensional array to four functions, using each one of the methods mentioned above.

two-dim-array.c

 

#include<math.h>

#include<stdio.h>

main()

{

int arr[ ][2]={{11,12},{21,22},{31,32},{41,42},{51,52}};

int i,m,n,*j;

            m=5;

            n=2;

 

            print(arr,m,n);

show(arr,m,n);

display(arr,m);

exhibit(arr,m);

}

 

Print (int b[ ][2],int l,int p)

{

            int i,j;

            printf("print \n");

 for(i=0;i<l;i++)

            {

                        printf("%d %d \n",b[i][0],b[i][1]);

            }

}

 

 

Show (int *b,int l,int p)

{

 int i,j;

 printf("show \n");

 for(i=0;i<l;i++)

            {

                        printf("%d %d \n",*(b+i*2+0),*(b+i*2+1));

             }

 }

display (int(*b)[2],int l)

{

int i,j,*p;

printf("display \n");

for(i=0;i<l;i++)

            {

                         p=(int*)b;

                         printf("%d %d \n",*(p),*(p+1));

                        b++;

            }

}

 

exhibit (int (*b)[5][2],int l)

{

int i,j;

printf("exhibit \n");

for(i=0;i<l;i++)

 {

                        printf("%d %d \n",(*b)[i][0],(*b)[i][1]);

             }

}

 

The functions, print, show, display and exhibit,are used to demonstrate  how the arrays are passed as arguments, the function call is the same but the way the arguments are received in the function definition is different. Print gets it as an array, display gets it as a one dimensional pointer array,  show gets it as a pointer and exhibit as a two dimensional pointer array. All the functions do exactly the same thing, that is to print out the elements of the array. Note the different ways the elements are accessed inside each of these functions.

In the function show the two dimensional array is received as pointer. On incrementing this pointer we can access each of the elements of the two-dimensional array . The elements of the array are read row by row.

In the function display the two dimensional array is received as a one dimensional pointer array b.  By default this pointer array, which had the same number of elements as the number of columns in out two dimensional array, is pointing to the first row of the two dimensional array. To access this elements we now need another pointer, which is defined here as p . We then  make the assignment p=(int*)b inside this function and read the two elements of the first row.  When we increment b by b++ we go to the next row and so on .

In the case of function exhibit each element of the two dimensional array of pointers is pointing to the corresponding element of array. Please note the use of paranthesis to read the elements of this array of pointers.

 

Returning  Array from a Function

While there are four different way to pass a two dimensional array to a function , there are only three different ways to return a two dimensional array from a function. They are,

 

 As in the earlier case we will look at a sample program to understand this better.

Return-array.c

 

#include<math.h>

#include<stdio.h>

#define l 4

main( )

{

int *a,i;

int *matrix1( );

int (*b)[2];

int *r;

int (*matrix2( ))[2];

int (*c)[5][2];

int (*matrix3( ))[5][2];

 

            a=matrix1( );

             printf("matrix1 \n");

            for(i=0;i<l;i++)

                        printf("%d %d \n", *(a+i*2),*(a+i*2+1));

b=matrix2( );

            printf("matrix2 \n");

            for(i=0;i<l;i++)

            {

                        r=(int*)b;

                        r=(int*)b;

                        printf("%d %d \n",*r,*(r+1));

                        b++;

             }

 

c=matrix3( );

printf("matrix3 \n");

for(i=0;i<l;i++)

            {

                        printf("%d %d \n",  (*c)[i][0],(*c)[i][1]);

            }

}

 

 

int * matrix1( )

 {

            static int arr[ ][2]={{11,12},{21,22},{31,32},{41,42},{51,52}};

 return *arr;

 }

 

 int (*matrix2( ))[2]

 {

 static int arr[ ][2]={{11,12},{21,22},{31,32},{41,42},{51,52}};

            return arr;

 }

 

 

 int (*matrix3( ))[5][2]

 {

            static int arr[ ][2]={{11,12},{21,22},{31,32},{41,42},{51,52}};

            return (int(*)[5][2])arr;

 }

 

Here in the function matrix1, which is declared as a pointer,  we have a two dimensional array with 5 rows and 2 columns and it returns this array as a pointer.  To receive it the main function should have an integer pointer.  That is in the statement a=matrix1( );  in the main function a should  an integer pointer.  What is returned here is the address to the array arr.

 

In the function matrix2, which is declared as a one dimensional array of pointers, the return statement is quite different from the other two. Here array is returned by simply its name.  In the main program the statement

b=matrix2( ); receive it through the one dimensional pointer b.

 

In matrix3 which is declared as a two dimensional array of pointers, the return statement should specify the full dimension of the array and its variable type. It is received in the main program by a two dimensional pointer array. 

 

In all the  cases  with pointers and arrays we have looked at so far, the array dimension was fixed. However there are cases in which we would like to change the dimension of the arrays dynamically. We will look at this aspect of pointes in the next lecture.

Next: Dynamic memory Allocation Up: Main Previous:Pointers