Tyler Durden
Tyler Durden

Reputation: 11532

Expected Specifier Qualifier List error inside struct

I am getting the error

error: expected specifier-qualifier-list before ‘(’ token

On the line beginning with (const char)* below and I do not understand what this error means. What does it mean? (Note that there is another question on this topic, but the one answer in the other question does not explain what the error means.)

#include "stddef.h"        /* size_t */

typedef struct {
     size_t size;
     (const char)* strings[];
} STRLIST;

static STRLIST listMediaType = {
     7,
     {
         "Book",
         "Map",
         "Booklet",
         "Pamphlet",
         "Magazine",
         "Report",
         "Journal"
     }
 };

Upvotes: 1

Views: 434

Answers (2)

user3386109
user3386109

Reputation: 34829

You can sort of fix the problem by removing the parentheses. That's only sort of fixing the problem because the C specification forbids the initialization of flexible array members with initializer lists. However, clang (and I assume gcc) allow it as an extension. If you compile with -Weverything, you should get a warning that you're using an extension.

As for why the parentheses are not allowed: to truly understand that you need to read section "6.7 Declarations" in the specification, which is 38 pages long. Short answer is that the compiler was expecting a declaration. A declaration starts with a variety of specifiers and qualifiers, none of which start with parentheses.

So the proper interpretation of the error message (from the compiler's point of view) is:

"I was expecting the declaration of a structure member, which should start with a specifier or qualifier, but instead you gave me a parentheses. Don't do that."

Upvotes: 1

user3629249
user3629249

Reputation: 16540

this particular error message is due to the extra parens around the type +modifier I.E. the extra parens around const char.

Remove the extra parens and all the errors go away.

Note there will still be a warning about the listMediaType being defined but not used.

Upvotes: 0

Related Questions