14.15 – pointers and matrix – Part-1

by subbu on January 28, 2014

Pointer array:

Set of elements of the same type stored in adjacent memory locations under the same name is called array. Like integer, floating point or a character array even a pointer array can be declared so that individual elements of array could refer different memory locations.

short *x;

It is a pointer which is capable to store the address of a single short type variable

short *x[5];

It is a pointer array with 5 pointers of short type where every element could store the address of another short type memory location

Now every element of array is a pointer, can point other memory locations of same type

short a=10,b=20,c=30,d=40,e=50;
short *x[5];

x[0]=&a;
x[1]=&b;
x[2]=&c;
x[3]=&d;
x[4]=&e;

Here x is a pointer array with 5 pointer elements, every element point different variables. Now the name of pointer array x gives the address of base pointer (65506), x+1 gives the address of second pointer and x+2 gives the address of third pointer.

By using address arithmetic we can access every variable through x. We will see how it can be done

Accessing a though x using address arithmetic
x+0 is 65506
value at 65506 or *(x+0) is 65524 which is the address of a
value at 65524 or *(*(x+0)) is 10

Accessing b through x using address arithmetic
x+1 is 65508
value at 65508 or *(x+1) is 65522 which is the address of b
value at 65522 or *(*(x+1)) is 20

pointer array

#include<stdio.h>
int main()
{
short a=10,b=20,c=30,d=40,e=50;
short *x[5];
short i;
x[0]=&a,x[1]=&b,x[2]=&c,x[3]=&d,x[4]=&e;
for(i=0;i<5;i++)  /* Accessing every element through pointer to pointer */
   printf("%5d",*(*(x+i)));
return 0;
}

Output:
10   20   30   40   50

Address arithmetic with two dimensional array:

We know that, an array is a set of elements of the same type stored in adjacent memory locations. A two dimensional array is a set of single dimensional arrays of same size and type stored in adjacent memory locations.

Now we will see how a two dimensional array is actually managed in the memory

When a two dimensional array is declared then the compiler allocates the memory allocation of individual rows or single dimensional arrays in adjacent memory locations. The name of matrix gives the address of first row or first single dimensional array.

short x[3][5]={{5,6,10,12,13},{21,31,22,45,55},{7,14,17,71,33}};

address arithmetic with matrix

#include<stdio.h>
void display(short * *);
int main()
{
short x[3][5]={{5,6,10,12,13},{21,31,22,45,55},{7,14,17,71,33}};
printf("%u",x);                 /* name of matrix */
printf("\n%u",&x[0][0]);   /* address of first element */
printf("\n%u",&x[0]);        /* address of first row */
return 0;
}

Output:
65596
65596
65596

By the above output, we can conclude that the name of matrix gives the address of first row

#include<stdio.h>
int main()
{
short x[3][5]={{12,34,13,44,21},{55,18,23,22,65},{21,44,55,65,12}};
printf("%u",x);
printf("\n%u",x+1);
printf("\n%u",x+2);
return 0;
}

Output:
65596
65606
65616

Now x gives the address of first row (65496), x+1 gives the address of second row (65506) and x+2 gives the address of third row (65516)

Now x could access every element of matrix using address arithmetic. Let us look at some examples

Accessing x[0][2] using address arithmetic
x[0] or *(x+0) is 65596
65596+2 or *(x+0)+2 is 65600
value at 65600 or *(*x+0)+2) is 10

Accessing x[1][3] using address arithmetic
x[1] or *(x+) is 65606
65606+3 or *(x+1)+3 is 65612
value at 65612 or *(*x+1)+3) is 45

By looking at the above examples, we can conclude that every element of matrix can be accessed either by using subscript or by using address arithmetic

matrix formula for accessing

To read the data from the keyboard using scanf() and store into the matrix, we can use either subscript or pointer arithmetic

formula to read into matrix

#include<stdio.h>
int main()
{
short x[3][5]={{5,6,10,12,13},{21,31,22,45,55},{7,14,17,71,33}};
short i,j;
printf("Elements of matrix:\n");
printf("Using Subscript...:\n");
for(i=0;i<3;i++)
{
  for(j=0;j<5;j++)
    printf("%5d",x[i][j]);
  printf("\n");
}
printf("Elements of matrix:\n");
printf("Using pointer arithmetic...:\n");
for(i=0;i<3;i++)
{
  for(j=0;j<5;j++)
    printf("%5d",*(*(x+i)+j));
  printf("\n");
}
return 0;
}

Output:
Elements of matrix:
Using Subscript…:
5    6   10   12   13
21  31  22   45   55
7   14  17    71   33

Elements of matrix:
Using pointer arithmetic…:
5    6   10   12   13
21  31  22   45   55
7   14   17   71   33

Specification: Accept a nxm matrix and print the sum of all the elements using pointer arithmetic

#include<stdio.h>
int main()
{
short x[50][50],n,m,i,j,sum;
printf("Enter the class of matrix:\n");
scanf("%d%d",&n,&m);
printf("Accept %dx%d matrix:\n",n,m);
for(i=0;i<n;i++)
  for(j=0;j<m;j++)
    scanf("%d",*(x+i)+j);
for(sum=0,i=0;i<n;i++)
  for(j=0;j<m;j++)
     sum=sum+*(*(x+i)+j);
printf("Sum of all the elements %d",sum);
return 0;
}

Execution:
Enter the class of matrix:
3 5
Accept 3×5 matrix:
10 20 30 40 50
50 40 30 20 10
10 30 20 50 40
Sum of all the elements 450

Previous post:

Next post: