C - 警告:赋值从指针生成整数而不进行强制转换

时间:2016-02-04 05:00:45

标签: c

我正在尝试编写一个程序,它将字符串的偶数和奇数索引放入它们自己的数组中(每个数组的末尾都有空终止符),它们存储在一个数组中:

char **result = malloc(sizeof(char*) * 2);
int a, b = 0;
int index = strlen(s) / 2;
if (strlen(s) % 2 == 1) {
    result[0] = malloc(sizeof(char) * (index + 2));
    result[1] = malloc(sizeof(char) * (index + 1));
    result[0][index + 1] = "\0"; // 1
    result[1][index] = "\0";     // 2
} else {
    result[0] = malloc(sizeof(char) * (index + 1));
    result[1] = malloc(sizeof(char) * (index + 1));
    result[0][index] = "\0"; // 3
    result[1][index] = "\0"; // 4
}
for (int i = 0; i < strlen(s); i++) {
    if (i % 2 == 0) {
        result[0][a] = s[i];
        a++;
    } else {
        result[1][b] = s[i];
        b++;
    }
}
return result;

当我编译它时,注释行会收到警告“赋值使指针从没有强制转换的指针生成整数”。我不明白这段代码有什么问题。帮助

1 个答案:

答案 0 :(得分:1)

在以下作业中 -

result[0][index + 1] = "\0"; // 1
result[1][index] = "\0";     // 2

使用单引号' '而不是双引号。

""用于字符串文字,其中result[1][index]char类似,因此,您会收到警告。

result[0][index + 1] = '\0';      /*  <-- assigning character   */