1

This is a simplification of my situation:

header.h

#DEFINE NUMBER 3
extern char reserved[][];

definer.c

char reserved[NUMBER][4] = {"WOW","LOL","K"}

sign.c

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

in sign.c, I get the error Expression must be a pointer to a complete object type for the word reserved

What do you suggest me to do?

2 Answers2

5
  1. The error message says that you cannot usefully use the declaration extern char reserved[][]; because the compiler needs at least the second dimension of the array to know how to access parts of the array — so it needs to be extern char reserved[][4];. With that declaration, it is immediately clear that "I love Stack Overflow" (regardless of word breaks and capitalization) is far too long to be equal to any of the strings in the array, but that's somewhat incidental.

  2. You can't usefully compare strings like that — you need to use strcmp(). See How do I check if a value matches a string? and How do I properly compare strings? amongst many other possible SO questions.

    You have:

    if (reserved[1] == "I love stackoverflow")
    

    You need:

    if (strcmp(reserved[1]), "I love Stack Overflow") == 0)
    

    or equivalent. Explicitly comparing the result of strcmp(A, B) with 0 using any op from the set ==, !=, >=, <=, > or < matches the result you would get from A op B if strings were real built-in types in C.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

There are problems with your code:

  1. missing parameter list, even if empty, you must provide parentheses in function definition;

  2. String comparison in C must be performed by usage of function strcmp(). You cannot use == operator for strings.

    #include <string.h>

    #define NUMBER 3

    char reserved[NUMBER][4] = { "WOW", "LOL", "K" };

    void theFunctionWithTheError(void){

       if (strcmp("I love stackoverflow", reserved[1]) == 0)
       {
          return;
       }
   }
  • You've compressed the code into one file, which removes the problem without explaining why it was a problem or explaining how to avoid the problem when there must be multiple files. That kinda avoids the point of the question. Also, your call to `strcmp()` inverts the test from `==` to `!=`. – Jonathan Leffler Aug 12 '20 at 23:28
  • There are multiple errors in his code and I tried to focus on I thought it was important and missed the error in the variable declaration. Some errors are still present such as NUMBER macro not being visible in define.c. But thanks for the feedback. – João Pedro Nascimento Aug 12 '20 at 23:41