Hey all,
I'm trying to implement a function InitMatrix which suppose to return a pointer to a new created 3d array of the size determined by x,y and z.
I have a struct which represents a matrix and should be able to deal with some functions required, like setting the range of the matrix.. etc..
typedef struct Struct_Matrix {
int ***array;
//..
//..
} Struct_Matrix;
Struct_Matrix *InitMatrix(int x, int y, int z) {
}
I think I know how to allocate a 3D array:
int x, y, z;
int i,j,k;
int ***array=(int ***)malloc(x*sizeof(int**));
for(i=0;i<x;i++) {
array[i]=(int**)malloc(y*sizeof(int*));
for(j=0;j<y;j++) {
array[i][j]=(int*)malloc(z*sizeof(int));
}
}
I have 3 questions:
1. Is my allocation correct?
2. How can I do it with the function I gave and the struct? what should I return? How to allocate a memory to the struct itself and then its fields?
3. How do I free the memory allocated by this function? lets say by a function called
void Free(array *free);
Thanks!