14.16 – Pointers and Matrix Part-2

by subbu on January 29, 2014

Sending the address of a two dimensional array as argument

While sending a single dimensional array as argument, we write the name of array as actual argument and a pointer is declared as a formal argument to store the address of first element.

When we write the name of two dimensional array or a matrix as actual argument, it is the address of first row that would be sent as actual argument. Because the row is a single dimensional array, a pointer to single dimensional array must be declared as a formal argument.

sending address of matrix as argument

display(x);

The name of matrix is written as actual argument.

void display(short (*p)[5])
{

}

Here p is not a normal pointer, it is a pointer to a row of 5 elements. p+0 gives the address of first row 65596, p+1 gives the address of next row after 5 short elements and p+2 gives the address of third row.

short q;
for(i=0;i<3;i++)
{
  q=p+i;
}

The above loop assigns address of each row to the pointer q for each iteration. Once q got the address of a base element of a row then it can access every element of row using address arithmetic

for(j=0;j<5;j++)
   printf("%5d",*(q+j));

Putting all together as a function

void display(short (*p)[5],short n,short m)
{
 short i,j;
 short *q;
 for(i=0;i<n;i++)
 {
   q=p+i;
   for(j=0;j<m;j++)
     printf("%5d",*(q+j));
   printf("\n");
 }
}

Program:

#include<stdio.h>
void display(short (*q)[5],short,short);
int main()
{
 short x[3][5]={{12,34,13,44,21},{55,18,23,22,65},{21,44,55,65,12}};
 display(x,3,5);
 return 0;
}
void display(short (*p)[5],short n,short m)
{
 short i,j;
 short *q;
 for(i=0;i<n;i++)
 {
  q=p+i;
  for(j=0;j<m;j++)
   printf("%5d",*(q+j));
  printf("\n");
 }
}

Output:
12   34   13   44   21
34   13   44   21   55
13   44   21   55   18

Method 2:

In the above method, pointer to a row of 5 elements is declared to accept the address of a matrix. Even we can define a pointer to pointer to accept the address of a two dimensional array. We will see why is it so?

A normal pointer is used to store the address of a single dimensional array because the address of a single dimensional array is an address of the first element which is again a normal variable

short x[5];
short *p=&x;

A pointer to pointer must be used to store the address of a two dimensional array because the address of a two dimensional array is not the address of a normal element but, the address of a single dimensional array.

short x[3][5];
short **p=x;

Here the function display() can also be written as

void display(short **p,short n,short m)
{
 short i,j;
 short *q;
 for(i=0;i<n;i++)
 {
   q=p+i;
   for(j=0;j<m;j++)
      printf("%5d",*(q+j));
   printf("\n");
 }
}

Program:

#include<stdio.h>
void display(short**,short,short);
int main()
{
short x[3][5]={{12,34,13,44,21},{55,18,23,22,65},{21,44,55,65,12}};
display(x,3,5);
return 0;
}
void display(short **p,short n,short m)
{
 short i,j;
 short *q;
 for(i=0;i<n;i++)
 {
  q=p+i;
  for(j=0;j<m;j++)
   printf("%5d",*(q+j));
  printf("\n");
}
}

Output:
12   34   13   44   21
34   13   44   21   55
13   44   21   55   18

Returning the address of a matrix:

Like the address of a single dimensional array even the address of a two dimensional array can be returned to the caller. Define the matrix as static type to keep the scope even after returning the address and leaving the function scope.

returning the address of a matrix

Program:

#include<stdio.h>
short** source();
int main()
{
 int i,j;
 short **p;       /* To hold the address of matrix    */
 short *q;         /* To hold the address of each row */
 p=source();      /* receiving the address of matrix  */
 for(i=0;i<3;i++)
 {
  q=p+i;                 /* assigning the address of each row */
  for(j=0;j<5;j++)
    printf("%5d",*(q+j)); /* accessing the elements of each row */
  printf("\n");
 }
return 0;
}
short** source()
{
 static short x[3][5]={{12,34,13,44,21},{55,18,23,22,65},{21,44,55,65,12}};
 return x;         /* returning the address of matrix */
}

Output:
12   34   13   44   21
34   13   44   21   55
13   44   21   55   18

Previous post:

Next post: