
February 24th, 2003, 10:53 PM
|
|
Junior Member
|
|
Join Date: Feb 2003
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Dynamic 2d Arrays
I believe that the following code is correct for creating a dynamic 2d array K(which is a **int) :
int **K;
int n;
/* Create a pointer to each row of the matrix. */
K = (int **)malloc(size * sizeof(int *));
/* Create each row of the matrix */
for(n = 0; n < size; n++)
K[n] = (int *)malloc(size * sizeof(int));
My question is: If I refer to an element in K, is it valid to use subscripts(ex. K[1][1])? Is the memory region created by my code analgous to the memory region generated by a statitic 2d
declaration(ex. int K[size][size])? I have had no problems yet, however I am worried that I will find a problem later.
Thanks
|