c - 将指针设置为 NULL 会产生 "assignment makes integer from pointer without a cast"警告

标签 c

我阅读了有关它的其他问题,但仍然无法理解是什么导致了我收到的警告。

char* func(const char* oldList) {
    static char* newList = NULL;

    if (oldList == NULL) {
        *newList = NULL;
    }
    ...
}

我收到的警告是“赋值从指针生成整数而不进行强制转换”(对于行*newList = NULL)。为什么?

最佳答案

newList 是一个指向字符的指针。

*newList 是指针指向的字符。

因此,分配*newList = NULL尝试将指针(NULL)分配给字符 - 从而进行强制转换。

在此特定代码中,即使您进行某种类型的转换,仍然会出现段错误,因为 newList = NULL

您可能想写:

if (oldList == NULL) {
    newList = NULL;
}

关于c - 将指针设置为 NULL 会产生 "assignment makes integer from pointer without a cast"警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41543197/

相关文章:

c# - C api 到 C# GUI

c - 从邻接矩阵中删除顶点的好方法

c - SQLite3错误: 'near "UP": syntax error'?

c - 为什么使用偏移密码加密后文件大小变小了?

c - 海龟用 openGl 绘制分形

c - GNU 读取线 : enormous memory leak

c - .h 文件中声明的函数在哪里找到它的定义?

c - 无法从 C 文本文件中读取结构

c - 无法理解C和类型转换中的指针

c - 如何检查数组中是否存在(子)数组