【问题标题】:c error: assignment makes integer from pointer without a cast [-Werror=int-conversion]c错误:赋值从指针生成整数而没有强制转换[-Werror=int-conversion]
【发布时间】:2020-05-17 12:37:19
【问题描述】:

当我尝试编译我的代码时,我不断收到错误“赋值从指针中生成整数而没有强制转换 [-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 为整数指针,将 newSunkList 声明为 int

标签: c


【解决方案1】:

线

    int* sunkList, newSunkList;

sunkList 声明为整数指针 (int *),将 newSunkList 声明为普通的 int,因此警告:

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

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

int *sunkList, *newSunkList;

或:

int *sunkList;
int *newSunkList;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-29
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    相关资源
    最近更新 更多