A question about struct assignment!

E

ehui928

/* test.c */

struct S
{
int a;
int b;
};

/*
The following 3 statements will cause compile error
when compiled under MinGW gcc3.4.2,the error message is:
"error: syntax error before '.' token"
*/
struct S ss;
ss.a = 1;
ss.b = 2;


int main()
{
printf("%d, %d\n", ss.a, ss.b);
return 0;
}



However, when I move the two struct assignment statements
into main function, as follows:

int main()
{
ss.a = 1;
ss.b = 2;
printf("%d, %d\n", ss.a, ss.b);

return 0;
}

it will be successfully compiled!
I have been greatly puzzled!!!
Can anyone explain the reason to me?
Thanks!
 
K

Keith Thompson

ehui928 said:
/* test.c */

struct S
{
int a;
int b;
};

/*
The following 3 statements will cause compile error
when compiled under MinGW gcc3.4.2,the error message is:
"error: syntax error before '.' token"
*/
struct S ss;
ss.a = 1;
ss.b = 2;


int main()
{
printf("%d, %d\n", ss.a, ss.b);
return 0;
}



However, when I move the two struct assignment statements
into main function, as follows:

int main()
{
ss.a = 1;
ss.b = 2;
printf("%d, %d\n", ss.a, ss.b);

return 0;
}

it will be successfully compiled!

Statements are legal only inside function definitions.
 
R

Richard Tobin

ehui928 said:
/*
The following 3 statements will cause compile error
when compiled under MinGW gcc3.4.2,the error message is:
"error: syntax error before '.' token"
*/
struct S ss;
ss.a = 1;
ss.b = 2;

Try simplifying:

int a;
a = 1;

Does the problem go away?

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top