Log in

View Full Version : Struct member assignment (c-only)


emorrp1
Jun 24, 2008, 07:17 AM
Hi, I'm now in a job where I have to code in pure C (not C++) and am having trouble adjusting to the differences, hope you can help. As soon as I un-comment any one of the commented lines in the code snippet below, the program fails to compile with a syntax error on the "int a=0..." line after the commented block (This line isn't a problem while the rest is commented out). I haven't been able to identify the problem, even after searching the internet, am I missing something about the assignment operator/structs/arrays in C?



typedef struct touch_switch {
int p_loc;
} touch_switch_t;

void touch_switches_set(touch_switch_t *switches[]);



void main(void) {
touch_switch_t S[8];

/* S[0].p_loc = 0x0;
S[1].p_loc = 0x1;
S[2].p_loc = 0x4;
S[3].p_loc = 0x5;
S[4].p_loc = 0x2;
S[5].p_loc = 0x3;
S[6].p_loc = 0x8;
S[7].p_loc = 0x9;*/
/* touch_switches_set(&S);*/

int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0;
}

void touch_switches_set(touch_switch_t *switches[]) {
(*switches)[0].p_loc = 0x0;
(*switches)[1].p_loc = 0x1;
(*switches)[2].p_loc = 0x4;
(*switches)[3].p_loc = 0x5;
(*switches)[4].p_loc = 0x2;
(*switches)[5].p_loc = 0x3;
(*switches)[6].p_loc = 0x8;
(*switches)[7].p_loc = 0x9;
}

emorrp1
Jun 24, 2008, 08:54 AM
The solution (from another board) is:

"The reason you get an error on the int a ... is that you are not defining your variables before the start of code. In C++ you can literally put a variable definition anywhere any statement can go. Some C compilers allow this too, but far from all. So as soon as you start writing code statements (anything that isn't a type, function or variable declaration/definition), you can not declare any further variables in that block.

Your function prototype is also missing a () set. "