struct initialization in C -
before mark duplicate/downvote, i have read books, spend decent amount of time on internet researching this, cannot find answer.
i want initialize struct when create it. want declare type using typedef
here trying do
typedef struct clock_tag clock; struct clock_tag{ int time; }clock = { 0 };
and gives me error "redefinition of 'clock' different kind of symbol"
typedef struct clock_tag{ int time; }clock = { 0 };
gives "illegal initializer (only variables can initialized)"
i know have use name of struct in order initialize it, want initialize @ time of it's creation, please not suggest having init() method.
this example code, i want understand how can have typedef , struct initialization in .h file know there many ways around this, can omit using typedef or initialize struct members other way, want understand why gives me error , how fix it.
p.s legal malloc struct in .h file?
a typedef
alias, when stating:
typedef struct clock_tag clock;
and stating after typedef:
struct clock_tag{ int time; } clock = {0}; ^^^^^
you're trying initialize struct
name. meant was:
typedef struct clock_tag clock; struct clock_tag{ int time; } clock = {0};
Comments
Post a Comment