c - c错误:赋值从指针生成整数而没有强制转换[-Werror=int-conversion]
问题描述
当我尝试编译我的代码时,我不断收到错误“赋值从没有强制转换的指针中生成整数 [-Werror=int-conversion]”。我不确定发生了什么,因为我已经检查过所有内容都与变量类型 int* 匹配。
int main(void) {
int* sunkList, newSunkList;
char*** shipArray;
sunkList=createSunkList(nShips);
newSunkList=updateSunkList(sunkList, shipArray);
return 0;
}
newSunkList=updateSunkList(sunkList, shipArray) 是错误所在。
int* createSunkList(int nShips);
{
int i;
int* result=(int*)malloc(sizeof(int)*nShips);
for(i=0;i<nShips;i++)
result[i]=1;
return result;
}
int* updateSunkList(int* sunkList, char*** shipArray)
{
char** temp;
int i,j,k,a=0;
for(k=0;k<nShips;k++)
{
temp=shipArray[k];
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(temp[i][j] = 'S')
a=1; /* If 'S' is found then a turns to 1 */
}
}
if(a==0) /* If a==0 then no 'S' has been found so the ship has sunk */
sunkList[k]=0;
}
return sunkList;
}
解决方案
线
int* sunkList, newSunkList;
声明sunkList
为整数指针 ( int *
) 和newSunkList
normal int
,因此警告:
warning: assignment to ‘int’ from ‘int *’ makes integer from pointer without a cast [-Wint-conversion]
要修复错误,您应该如下声明两个变量:
int *sunkList, *newSunkList;
或者:
int *sunkList;
int *newSunkList;
推荐阅读
- swiftui - 如果操作未立即完成,则 SwiftUI 视图在绑定到 @Published var 时没有动画
- sql - 如何计算元素的平均值
- python - Pyqt5 两个QButton拖放
- c++ - 如何解决悬空的 const ref
- reactjs - 如何在运行时读取通过 docker-compose.yaml 文件传递的反应应用程序中的环境变量?
- angularjs - 是否可以在 Angular 1.7 中创建新的 Promise 对象?
- html - 搜索图标位于表单输入下方
- java - 如何从 Angular 应用程序打开桌面应用程序?
- swift - 将object3d放在海拔位置目标位置
- python - 如何将熊猫数据框从 praw 保存到 xlsx?