I have been trying to allocate a 2 dimensional array at run time using malloc
by using the concept of pointers to pointers. This code doesn't show any compilation error but gives a runtime error.
#include<stdio.h>
#include<stdlib.h>
int ** alpha(void)
{
int **x;
x=(int **)malloc(3*sizeof(int *));
for(int i=0;i<3;i++)
{
x[0]=(int *)malloc(3*sizeof(int));
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
x[i][j]=i+j;
}
}
return x;
}
int main()
{
int **p;
p=alpha();
printf("%d",p[1][2]);
return 0;
}