i wanted to know how would you create dynamically allocated 2D arrays. i understand how to create 1D arrays but don't know how to do that.
Thanks
i wanted to know how would you create dynamically allocated 2D arrays. i understand how to create 1D arrays but don't know how to do that.
Thanks
well do it for one, but twiceCode:int *ptr,x; ptr=malloc(10*sizeof int); if(!ptr){ perror("Something went wrong\n"); return 1; } for(x=0;x<10;x++){ ptr[x]=malloc(10*sizeof int); if(!ptr[x]){ perror("Something went wrong\n"); return 1; } }
In Java or C# they call that thing a jagged array because your first array stores pointers to other arrays, so your second dimension could be different for each element of the first one. If you look at the memory layout, it's not anything that could be interpreted as "2D" either, but maybe it's still called a 2d-array. Is it?Originally Posted by linuxdude
You're basically allocating a one dimensional array that is big enough to hold enough elements for the 2 dimensions you wanted. TBL_M is a constant for the size of the first dimension m. So for an array of the size [2][3] your flat array would hold its elements in this orderOriginally Posted by axe786
a[0][0], a[0][1], a[0][2], a[1][0], a[1][1], a[1][2]
main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }
linuxdude it's int **ptr;
byt anyway, it's quite limiting, because you are forced to use indexing. I wold probably go forCode:int *tbl = malloc(sizeof(*tbl)*TBL_N*TBL_M); tbl[n*TBL_M+m] = a; free(tbl);
http://www.eskimo.com/~scs/C-faq/q6.16.htmlOriginally Posted by axe786
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
whoops sorry about that didn't really pay attention.
Thanks for the reply guys, i'm just a little confused on this line:
wats (n),(m) and (a) and also what excatly does this statement do???Code:tbl[n*TBL_M+m] = a;
Thanks