Follow

In C if I have
typedef struct
{
Token_type type;
Token_data data;
} Token_t;

typedef struct sitem {
char *key;
struct Token_t *token;
} item_t;

Why can't I do?

void insert(char *key, Token_t *data) {
item_t* new_item = malloc(sizeof (_item_t));
new_item->token = data;

}

· · Web · 5 · 1 · 0
@matrix it works after I resolve the errors that gcc gives me, that it should be item_t and not _item_t, and that you need 'Token_t *token' and not 'struct Token_t *token'. The latter case because you're using the typedef

@matrix

What do you mean you can’t do it?

There’s a couple of minor errors but I’m not sure if that’s what you’re getting at.

struct Token_t is distinct from Token_t might be the more subtle error, in case that’s what you’re after.

Consider your item_t

typedef struct sitem { ... } item_t;

struct sitem and item_t are equivalent, but struct item_t would be something else.

@ademan @matrix You can go all Carmack and:

typedef struct item_s { ... } item_t;

@matrix
So you're initializing the item_t new_item, and giving it memory, but you're not initializing the Token_t token within it and expect it to directly pass to it a different memory allocation?

@matrix
Man, I really hate C's pointer stuff...
One problem might be that you need to do new_item->token = *data

@matrix sizeof(sitem) maybe? Haven't been around this for >10 years now but itemt is a pointer so it's size if 4/8 bytes?
Sign in to participate in the conversation
Game Liberty Mastodon

Mainly gaming/nerd instance for people who value free speech. Everyone is welcome.