C语言编程
代码:
struct battleship makeShip (int size, int pos)
{
int i, j;
int* body;
body = (int*) malloc (size * sizeof(int));
for (i = pos; i < (pos + size); i++){
for (j=0; j < size; j++){
body[j] = 1;
}
}
}
如果我尝试添加 return 0,则不确定是什么导致了错误;我得到:
错误:从结果不兼容的函数返回“int” 输入“结构战舰” 返回0;
最佳答案
你必须归还战舰结构。
如果您不需要返回任何内容,只需更改:
struct battleship
与:
void
意味着该函数不返回任何内容。
考虑到您在这里为某些东西分配了一些内存,您可能想返回 body 的地址,它是一个指向 int 类型的指针。
在这种情况下,您的函数应以以下内容开头:
int * makeShip(int size, int pos)
关于c - 警告 : control reaches end of non-void function [-Wreturn-type] },我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43212114/