By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 946 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

A question about struct assignment!

P: n/a
/* 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!

Sep 18 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"ehui928" <eh*****@gmail.comwrites:
/* 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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 18 '06 #2

P: n/a
In article <11*********************@m7g2000cwm.googlegroups.c om>,
ehui928 <eh*****@gmail.comwrote:
>/*
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
Sep 18 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.