Consider the following program include include define size 3
Solution
#include <stdio.h>
#include <stdlib.h>
#define size 3
void func(int **a)
{
int tmp;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
tmp = *(*(a+i)+j); //swapping rows and columns using temp
*(*(a+i)+j) = *(*(a+j)+i);
*(*(a+j)+i) = tmp;
}
}
}
int main(void)
{
int **arr = malloc(sizeof(int*)*size); // allocate memory for 3(size) pointers to pointers(**arr)
for(int i=0;i<size;i++)
{
arr[i] = malloc(sizeof(int)*size); // allocate memory for 3 rows
for(int j=0;j<size;j++)
{
arr[i][j] = 2*i + 3*j;
//a[0][0] = 0 a[0][1] = 3 a[0][2] = 6
//a[1][0] = 2 a[1][1] = 5 a[1][2] = 8
//a[2][0] = 4 a[2][1] = 5 a[2][2] = 10
}
}
//func(arr);
printf(\"%d | %d | %d | %d \ \",*(arr[1]+2),*(*(arr+2)+0),*(arr[2]+2),*(*arr+2));
//*(*(arr[1]+2) = 8 *(*(arr+2)+0) = 4 *(arr[2]+2) = 10 *(*arr+2)= 6
return 0;
}
Output: Explaination in comments
