Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

is my dynamically allocation of 2D array correct ?

include<stdio.h>

include<stdlib.h>

include<string.h>

main()
{
int a,b,i,j,k=0;
scanf("%d %d", &a,&b);
int (*ptr)[b];
ptr = malloc( sizeof(int[a][b]) );

for(i=0;i<a;i++)
{
    for(j=0;j<b;j++)
    {
        ptr[i][j]=k;
        k++;
    }
}
for(i=0;i<a;i++)
{
    for(j=0;j<b;j++)
    {
       printf("%d\t",ptr[i][j]);
    }
       printf("\n");
}
free(ptr);
return 0;

}

Sign In or Register to comment.