0

I am trying to make a struct with a default value, as described here: Default values in a C Struct. However, I have this C code, inside a header file:

/* tokens.h */
typedef struct {
    char *ID;
    char *KEY;
    char *TYPE;
} tokens;

const struct tokens TOKENS_DFLT = {
    "id",
    "key",
    "type"
};

And I am getting an error on line 7 saying:

error: variable 'TOKENS_DFLT' has initializer but incomplete type

Can anyone please explain to me what this problem is and how I can fix it and prevent it in the future?

2 Answers 2

3

You haven't defined struct tokens. You've defined an unnamed struct and simultaneously typedef-ed it to the type name tokens.

If you had instead defined

typedef struct tokens_ {
    char *ID;
    char *KEY;
    char *TYPE;
} tokens;

Then you could declare your constant as either:

const struct tokens_ TOKENS_DFLT = { ... };

Or

const tokens TOKENS_DFLT = { ... };

As it is, you're between two stools.

Sign up to request clarification or add additional context in comments.

1 Comment

now i get a warning: Useless storage class specifier in empty declaration
2

This:

const struct tokens TOKENS_DFLT = {
    "id",
    "key",
    "type"
};

should be:

const tokens TOKENS_DFLT = {
    "id",
    "key",
    "type"
};

Since you've defined the name tokens to mean struct tokens.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.