表达式必须是指向完整对象类型的指针,为什么在这种情况下会出现此错误?

Sta*_*pro 1 c ansi-c

这是我的情况的简化:

头文件.h

#DEFINE NUMBER 3
extern char reserved[][];
Run Code Online (Sandbox Code Playgroud)

定义器

char reserved[NUMBER][4] = {"WOW","LOL","K"}
Run Code Online (Sandbox Code Playgroud)

符号.c

#include "header.h"
void theFunctionWithTheError{
    
       if (reserved[1] == "I love stackoverflow"){ /**THE LINE OF THE ERROR*/
       return;
    }
}
Run Code Online (Sandbox Code Playgroud)

在 sign.c 中,我收到错误Expression must be a pointer to a complete object type for the wordreserved

你建议我做什么?

Jon*_*ler 5

  1. 错误消息说您不能有用地使用声明,extern char reserved[][];因为编译器至少需要数组的第二维才能知道如何访问数组的一部分 - 所以它必须是extern char reserved[][4];. 有了这个声明,很明显"I love Stack Overflow"(不考虑分词和大小写)太长而不能等于数组中的任何字符串,但这有点偶然。

  2. 你不能有用地比较这样的字符串——你需要使用strcmp(). 请参阅如何检查值是否与字符串匹配?以及如何正确比较字符串?在许多其他可能的 SO 问题中。

    你有:

    if (reserved[1] == "I love stackoverflow")
    
    Run Code Online (Sandbox Code Playgroud)

    你需要:

    if (strcmp(reserved[1]), "I love Stack Overflow") == 0)
    
    Run Code Online (Sandbox Code Playgroud)

    或同等学历。比较明确的结果strcmp(A, B)0使用任何op来自集==!=>=<=><结果,你会得到从匹配A op B如果字符串是真正的内置类型C.