我的函数的目的是在堆上生成一个整数的二维array.在创建了指向整个数组的指针后,我将指向整个数组的指针返回到一个空*.我觉得有一个更好的返回类型,但我似乎无法理解WRITE语法.
请让我知道,如果我可以做一个更好的返回类型,而不仅仅是一个空*.
void* create_matrix(int x, int y){
int i, j, count;
int(*matrix)[x] = calloc(y, sizeof *matrix); // a pointer to a full array of size x.
//matrix = address of array
//*matrix = address of first element of array; *(matrix + 1) = address of first element of second array
//**matrix = element that exists at the first element of array; *(*(matrix + 1) + 1) = second element of second array = matrix[1][1]
count = 0;
for(i = 0; i < y; i++){
for(j = 0; j < x; j++){
matrix[i][j] = ++count;
}
}
for(i = 0; i < y; i++){
for(j = 0; j < x; j++){
printf("%d\n", matrix[i][j]);
}
}
return matrix;
}