-
Storage classes
Code:
void mycreatedynamicstorage(int **MyArray)
{
by malloc -allocate some 2D storage for the variable above.
}
void populatestorage(int **MyArray)
{
Simply populate the sample.
}
int main(void)
{
int **MyArray;
mycreatedynamicstorage(MyArray);
populatestorage(MyArray);
}
My question comesdown to storage class. I have following set-up. The create routine works as it should and creates an array,
but when it is sent to the next function it seems that I get a segmentaion fault and it would appear the array has gone
missing. Should I use a storage class for this. Now these routines are tested and work if I dont not explicity call the arrays
in the function and place the int **MyArray out of the main and into global. Please help if you can, I would send the code, but
I amworking on Linux without a connection so have had to type in the salient points. Thanks
-
in order for mycreatedynamicstorage() to work, main() has to pass it a pointer to pointer to pointers (whew!) -- that's three stars, not two. That allows mycreatedynamicstorage() to change the pointer declared in main().
Code:
const int MaxX = 20;
const int MaxY = 20;
void mycreatedynamicstorage(int ***MyArray)
{
int i;
int ** ay = malloc(MaxY * sizeof(int *));
for(i = 0; i < MaxY; i++)
ay[i] = malloc(MaxX * sizeof(int));
*MyArray = ay;
}
or better yet, pass the x and y values
Code:
void mycreatedynamicstorage(int ***MyArray, int x, int y)
{
int i;
int ** ay = malloc(y * sizeof(int *));
for(int i = 0; i < y; i++)
ay[i] = malloc(x * sizeof(int));
*MyArray = ay;
}
-
Yet more cross-posting - sheesh
Lot of reading of the forum rules not being done.