Stepii
Stepii

Reputation: 142

Array in C struct declaration

Can you explain what this code does and how does struct declaration works?

enum tokens
{
    ARG, CHAR, INT, IF, ELSE, FOR, DO, WHILE,
    SWITCH, RETURN, EOL, FINISHED, END
};

[...]

struct commands
{
    char command[20];
    char tok;
}
table[] =
{
    "if", IF,
    "else", ELSE,
    "for", FOR,
    "do", DO,
    "while", WHILE,
    "char", CHAR,
    "int", INT,
    "return", RETURN,
    "end", END,
    "", END
};

Upvotes: 1

Views: 83

Answers (2)

ad absurdum
ad absurdum

Reputation: 21359

I would prefer to see table defined in a separate statement, like this:

struct commands
{
    char command[20];
    char tok;
};

struct commands table[] =
{
    "if", IF,
    "else", ELSE,
    "for", FOR,
    "do", DO,
    "while", WHILE,
    "char", CHAR,
    "int", INT,
    "return", RETURN,
    "end", END,
    "", END
};

The effect is the same. table[] is an array of struct commands elements, initialized with the values from the initializer list. The enum values have type int, with ARG getting a value of 0; each subsequent enum member has a value one greater than the one which precedes it. It must be expected that the enum will never grow so large that member values can't be stored in a char, so a char member in the struct is used to hold this value. This seems likely intended as a way to save memory by using a char member instead of an int member for tok, yet this would only have an impact in severely memory-constrained environments, or where many such commands structs are used.

Note that initializing this way may trigger warnings on some compilers:

warning: missing braces around initializer [-Wmissing-braces]

It would be better to place braces around the individual struct initializers:

struct commands
{
    char command[20];
    char tok;
}
table[] =
    {
        {"if", IF},
        {"else", ELSE},
        {"for", FOR},
        {"do", DO},
        {"while", WHILE},
        {"char", CHAR},
        {"int", INT},
        {"return", RETURN},
        {"end", END},
        {"", END}
    };

This will quiet the missing braces warning and add some clarity to the code. Another alternative would be to use the designated initializer syntax for even more clarity:

struct commands
{
    char command[20];
    char tok;
}
table[] =
    {
        {.command = "if",     .tok = IF},
        {.command = "else",   .tok = ELSE},
        {.command = "for",    .tok = FOR},
        {.command = "do",     .tok = DO},
        {.command = "while",  .tok = WHILE},
        {.command = "char",   .tok = CHAR},
        {.command = "int",    .tok = INT},
        {.command = "return", .tok = RETURN},
        {.command = "end",    .tok = END},
        {.command = "",       .tok = END}
    };

Upvotes: 2

Prasanna Kumar
Prasanna Kumar

Reputation: 368

As far as structure is considered, struct is a user defined data type . It allows a number of primitive data types to be grouped together under a single name . The word that is immediate next to the keyword struct is the structure name . Considering the example code that you provided, commands is the structure name . The variables in the structure can never be initialized a value . Syntax for declaring structure variable is

struct struct_name variable_name;

The variables within the structure can be accessed using the structure variable and dot(.) operator

commands.tok = 'a' ;

Upvotes: 1

Related Questions