judith
judith

Reputation: 825

Initialize structs issues on C

I'm getting this error during compilation:

"c:\command_line.h(17): error C2143: syntax error : missing ';' before '*' Note: C++ does not support default-int command_line.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int"

This my code:

command_line.h

typedef struct symbol
{
    char* sym_type;
    unsigned short address;
}symbol;


typedef struct symbol_map
{
    char** p_arr_keys;
    symbol* p_arr_values;
    int item_count;
    int array_mode;
    int copy_keys;
}symbol_map;

typedef struct params
{
    int data_counter;
    int code_counter;
    int line_counter;
    int command_len;
    int error_counter;
    int warning_counter;
    symbol_map* p_symbol_map; // (This is line 17- from the error msg)
    char* p_last_symbol
}params;

main.c

params config;
config.code_counter = 0;
config.data_counter = 0;
config.line_counter = 0;
config.command_len = 0;
config.command_first_char = EMPTY;
config.error_counter = 0;
config.warning_counter = 0;
config.p_last_symbol = NULL;
config.p_symbol_map = {NULL}; // (This is line 17- from the error msg)

Any idea what's wrong with this initialization?

 config.p_symbol_map = {NULL};

Upvotes: 0

Views: 1141

Answers (5)

Josh Petitt
Josh Petitt

Reputation: 9589

You cant do it like that. You have to initialize the config AFAIK. You can do this since you are assigning a pointer and not a struct.

config.p_symbol_map = NULL;

Upvotes: 0

Nathan822
Nathan822

Reputation: 198

There is no need for {} around NULL. Just using NULL or nullptr would do. Further, what is p_symbol_map? There is no such thing in your declaration of params. Also, in line 17, it should be wrtten as:

struct symbol_map* p_symbol_map. This is the reason there is an error on line 17.

Hope this helped.

Upvotes: 0

Maksim Skurydzin
Maksim Skurydzin

Reputation: 10541

A construct you have used config.p_symbol_map = {NULL} is a static initialization and is only allowed in a variable declaration. If you want to assign a NULL to p_symbol_map you can simply config.p_symbol_map = NULL.

A valid case for static initialization applied to a symbol structure would be something like this:

symbol sym = {
   NULL,
   0x42
};

updated:

Btw, you are missing a semicolon after char* p_last_symbol in a structure definition.


typedef struct params
{
    int data_counter;
    int 
    int line_counter;
    int command_len;
    int error_counter;
    int warning_counter;
    symbol_map* p_symbol_map; // (This is line 17- from the error msg)
    char* p_last_symbol  <----- need to add ; here
}params; 
 

Upvotes: 1

Giovanni Funchal
Giovanni Funchal

Reputation: 9200

Something is wrong there, you are assigning to p_last_symbol member which you didn't declare in params. Also, p_symbol_map is a pointer to something, you can either initialize it with NULL or make it point to something else (such as memory allocated with malloc).

Upvotes: 0

Aftnix
Aftnix

Reputation: 4599

You are not initializing a structure, you are initializing a pointer to a structure.

struct foo {
  int a;
  int b;
};

struct foo bar = {.a = 0, .b = 1};

But if you are declaring a pointer to it, then you don't have a memory block to initialize at first. So you can't use {} idiom to initialize a pointer to struct;

struct foo *bar;
bar = NULL

Upvotes: 0

Related Questions