首页 > 解决方案 > 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;
}

标签: c

解决方案


线

    int* sunkList, newSunkList;

声明sunkList为整数指针 ( int *) 和newSunkListnormal int,因此警告:

warning: assignment to ‘int’ from ‘int *’ makes integer from pointer without a cast [-Wint-conversion]

要修复错误,您应该如下声明两个变量:

int *sunkList, *newSunkList;

或者:

int *sunkList;
int *newSunkList;

推荐阅读