LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-28-2002, 01:55 PM   #1
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Rep: Reputation: 30
Can someone help me with the following C example?


Hi. I am just learning structures and in the following gcc keeps giving me a parse error for line 11.
fio.c:11: parse error before `.'

the code is >

#include <stdio.h>

struct coord
{
int x;
int y;
};

struct coord first;

first.x = 50;
first.y = 70;

int main(void)
{

printf("x = %d and y = %d\n", first.x, first.y);

return 0;

I don't see a parse error on or before line 11. Is there something else I'm doing wrong here?

thanks you
frustrated
 
Old 09-28-2002, 02:04 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
You can't do those assignments outside of a method, it's not the same as just plaing declaring a variable, move it inside the main() sub.

Last edited by acid_kewpie; 09-28-2002 at 02:05 PM.
 
Old 09-28-2002, 02:08 PM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Here you have versions that work:
Code:
#include <stdio.h>

struct coord
{
int x;
int y;
};

struct coord first;

int main(void)
{
first.x = 50;
first.y = 70;

printf("x = %d and y = %d\n", first.x, first.y);

return 0;

};
or
Code:
#include <stdio.h>

struct coord
{
int x;
int y;
};

struct coord first={50,70};

int main(void)
{

printf("x = %d and y = %d\n", first.x, first.y);

return 0;

};
 
Old 09-28-2002, 02:20 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
heh, you don't like making them think do you mara?
 
Old 09-28-2002, 02:33 PM   #5
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
? It's just easier to copy a code than to write what to do. I write faster in C than in any other language.
 
Old 09-28-2002, 02:50 PM   #6
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
thanks alot acid and Mara

Is there a book that explains things like 'WHY' I can't put these assignments outside of main() etc. The books I have give examples of things in a simplistic way without explaining the 'whys' of things.

Are there any "REALLY INDEPTH" C books out there that I could buy?
 
Old 09-28-2002, 04:19 PM   #7
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Maybe not 100% accurate, but I suppose it explanin this case: you can use instructions only in functions.
Code:
first.x = 50;
first.y = 70;
is a pair of instructions, so it cannot be outside a function.
From the other hand,
Code:
struct coord first={50,70};
is not an instruction, it's a declaration with values, so it can be outside main().
 
Old 09-28-2002, 11:37 PM   #8
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
thamks Mara
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration