c - assigning struct in switch not working -
in program, i'm trying create new struct based od switch statement, when so, compiler returns error:
syntax error before '{' token
on row position assignment
i'm using dev-c++ 4.9.9.2 ide (i think it's using mingw compiler). it's brother's programming assignment i'm helping him with, haven't seen c in few years, i'm rusty (and wasn't champion before either).
here's simplified code:
typedef enum{top_right = 0,top_left,bottom_right,bottom_left} diagonal_t; typedef struct { int row; int column; } position_t; ... void checkdiagonal(diagonal_t diagonal_to_check) { ... position_t position; switch(diagonal_to_check) { case top_right: position = {0,0}; //here's error, don't know how repair it. //how create new struct here without disrupting //switch? break; case top_left: position = {0,0}; break; .... } }
the var_of_type_struct = { init_value }
syntax works in definitions; not work in assignments.
three common ways deal are
- defining function initializes
struct
- defining function sets fields parameters pass, and
- assigning individual fields of
struct
.
approach 1:
void init_pos(position_t *p) { p->row = 0; p->column = 0; } ... case top_left: init_pos(&position); break;
approach 2:
void set_pos(position_t *p, int r, int c) { p->row = r; p->column = c; } ... case top_left: set_pos(&position, 0, 0); break;
approach 3:
case top_left: position.row = 0; position.column = 0; break;
Comments
Post a Comment