[英]How do I return a pointer to a struct from a function?
我无法理解为什么编译器给我以下错误:
level0.c:在函数“create_grid”中:level0.c:28:9:警告:从不兼容的指针类型返回 [-Wincompatible-pointer-types] 返回网格;
我试图返回一个指向我在函数中创建的 struct gridType 类型的结构的指针。 这也是函数期望返回的类型。
该函数的代码是:
struct gridType* create_grid(int length){
char** array = malloc(length * sizeof(*array));
for(int i = 0; i < length; i++){
array[i] = malloc(length * sizeof(array));
}
for(int i = 0; i < length; i++){
for (int j = 0; j < length; j++){
array[i][j] = '-';
}
}
struct gridType{
int length;
char** array;
};
struct gridType* grid = malloc(sizeof(struct gridType));
grid->length = length;
grid->array = array;
return grid;
}
您不能在函数中定义struct gridType
并期望能够返回它(供其他人查看)。
类型移动
struct gridType{
int length;
char** array;
};
在函数create_grid()
之外(之前create_grid()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.