473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

A question about struct assignment!

/* 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
2 1490
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael Birkmose | last post by:
Hi everyone, I am in a situation where I need to instantiate a struct which has no tag name: struct some_struct { struct { int a; } embedded_member; };
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
2
by: Immo Birnbaum | last post by:
Hi, I'm trying to solve a programming lab assignment for my college C programming course, but as they taught us two semesters of Java before teaching us any C, I'm having problems with all the...
47
by: sunglo | last post by:
Some time a go, in a discussion here in comp.lang.c, I learnt that it's better not to use a (sometype **) where a (void **) is expected (using a cast). Part of the discussion boiled down to the...
28
by: WaterWalk | last post by:
Hi, I'm haunted by 2 questions about struct copy. Though I searched the net, but still in confusion. 1. Does struct assignment copies every member including array members? For example, struct...
24
by: Grey Alien | last post by:
If I have the ff struct: struct A { unsigned int i; char s; } a, b; And use them in code like this:
21
by: Chad | last post by:
At the following url http://c-faq.com/lib/qsort2.html, they have the following Q: Now I'm trying to sort an array of structures with qsort. My comparison function takes pointers to structures,...
6
by: Gestorm | last post by:
Hi everyone, I have a problem. If I declare a struct with a const member, what will happen?For example: if I declared a struct like following: struct{ const int a; char c; }aStruct; then such...
3
by: bernd | last post by:
Hi folks, the code below "runs" with the following message: Before structure assignment Bus Error(coredump) What I am trying to do is to define an "outer" structure containing an inner one...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.