How to do it…

  1. Define a matrix of 10 rows and 10 columns as follows (you can have a bigger matrix if you wish):
int a[10][10]
  1. Enter the size of the rows and columns as follows:
    printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
  1. Allocate memory locations equal to r *c quantity for keeping the matrix elements as follows:
    ptr = (int *)malloc(r * c * sizeof(int));
  1. Enter elements of the matrix that will be assigned sequentially to each allocated memory as follows:
    for(i=0; i<r; ++i)
{
for(j=0; j<c; ++j)
{
scanf("%d", &m);
*(ptr+ i*c + j)=m;
}
}
  1. In order to access this matrix via a pointer, set a ptr pointer to point at the first memory location of the allocated memory block, as shown in Figure 5.30. The moment that the ptr pointer is set to point at the first memory location, it will automatically get the address of the first memory location, so 1000 will be assigned to the ptr pointer:

Figure 5.30
  1. To access these memory locations and display their content, use the *(ptr +i*c + j) formula within the nested loop, as shown in this code snippet:
for(i=0; i<r; ++i)
{
for(j=0; j<c; ++j)
{
printf("%d\t",*(ptr +i*c + j));
}
printf("\n");
}
  1. The value of the r row is assumed to be two, and that of column c is assumed to be three. With values of i=0 and j=0, the formula will compute as follows:
*(ptr +i*c + j);
*(1000+0*3+0)
*1000

It will display the content of the memory address, 1000.

When the value of i=0 and j=1, the formula will compute as follows:

*(ptr +i*c + j);
*(1000+0*3+1)
*(1000+1)
*(1002)

We will first get *(1000+1), because the ptr pointer is an integer pointer, and it will jump two bytes every time we add the value 1 to it at every memory location, from which we will get *(1002), and it will display the content of the memory location 1002.

Similarly, the value of i=0 and j=2 will lead to *(1004); that is, the content of the memory location 1004 will be displayed. Using this formula, the value of i=1 and j=0 will lead to *(1006); the value of i=1 and j=1 will lead to *(1008); and the value of i=1 and j=2 will lead to *(1010). So, when the aforementioned formula is applied within the nested loops, the original matrix will be displayed as follows:

Figure 5.31
  1. To display the transpose of a matrix, apply the following formula within the nested loops:
*(ptr +j*c + i))

Again, assuming the values of row (r=2) and column (c=3), the following content of memory locations will be displayed:

i

j

Memory address

0

0

1000

0

1

1006

1

0

1002

1

1

1008

2

0

1004

2

1

1010

 

So, upon applying the preceding formula, the content of the following memory address will be displayed as the following in Figure 5.32. And the content of these memory addresses will comprise the transpose of the matrix:

Figure 5.32

Let's see how this formula is applied in a program.

The transposemat.c program for displaying the transpose of a matrix using pointers is as follows:

#include <stdio.h>
#include <stdlib.h>
void main()
{
int a[10][10], r, c, i, j, *ptr,m;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
ptr = (int *)malloc(r * c * sizeof(int));
printf("\nEnter elements of matrix:\n");
for(i=0; i<r; ++i)
{
for(j=0; j<c; ++j)
{
scanf("%d", &m);
*(ptr+ i*c + j)=m;
}
}
printf("\nMatrix using pointer is: \n");
for(i=0; i<r; ++i)
{
for(j=0; j<c; ++j)
{
printf("%d\t",*(ptr +i*c + j));
}
printf("\n");
}
printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
{
for(j=0; j<r; ++j)
{
printf("%d\t",*(ptr +j*c + i));
}
printf("\n");
}
}

Now, let's go behind the scenes.