LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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
 
LinkBack Search this Thread
Old 09-02-2010, 08:17 AM   #1
thelink123
Member
 
Registered: Aug 2008
Location: India, Kerala
Distribution: openSUSE 11
Posts: 46

Rep: Reputation: 15
Assigning Structure object's variable outside main() gives parse error


Hi,

Kindly take a look at the code below :



Code:
#include <stdio.h>

struct test
{
   int i;
   int j;
}obj;


obj.i = 5;
obj.j = 10;


int main()
{

   return 0;
}
I am getting the error :
Code:
error: parse error before '.'
Kindly tell me why i am getting this error. I know the error is occuring because i have assigned values to obj.i and obj.j outside main(). But i want to know why do that result in an error. From my part i have created an object 'obj' of stucture 'test' and assigned values to its variables. Where am i going wrong. Expecting a detailed explanation.

Last edited by thelink123; 09-02-2010 at 08:27 AM. Reason: Moved the header inclusion into the code area
 
Old 09-02-2010, 11:01 AM   #2
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 217

Rep: Reputation: 21
Yes, outside of main or function is static memory area (section BSS or similar) so you can not code there or assign something:
Code:
struct
{
   int i=5;   // forbidden
   int j;
} obj;

void f1()   // gone to code segment - works!
{
obj.i = 5;
obj.j = 10;
};
 
Old 09-02-2010, 11:17 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,402

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
Quote:
Yes, outside of main or function is static memory area (section BSS or similar) so you can not code there or assign something:
I am interested in this question raised by OP but not sure your answer is 'completely' correct, otherwise how would you create a global variable??
Code:
int i = 7;

main(){

printf("%d",i);
return 0;
}
Not pretty perhaps and I definitely do not like global variables when it can be helped, but this works just fine.
 
Old 09-02-2010, 11:25 AM   #4
bastl
Member
 
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 217

Rep: Reputation: 21
I can imagine that int is not handled as object so to get an object like struct do:
Code:
int *i = new int;

i=7;   // same error - forbidden
 
Old 09-02-2010, 01:18 PM   #5
johnsfine
Senior Member
 
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,012

Rep: Reputation: 731Reputation: 731Reputation: 731Reputation: 731Reputation: 731Reputation: 731Reputation: 731
Quote:
Originally Posted by thelink123 View Post
I know the error is occuring because i have assigned values to obj.i and obj.j outside main().
The problem is that you have execution time assignments that are not in any function. (The function doesn't need to be main).

You can assign values to global variables as part of defining the variables as long you use the correct syntax for setting values as part of the definition.

Code:
#include <stdio.h>

struct test
{
   int i;
   int j;
}obj = { 5, 10 };
As you can see, the correct syntax for setting structure members as part of definition of an instance of the structure is a rather ugly syntax (the association of values to members is by sequence rather than name). But that is the syntax C provides.

Last edited by johnsfine; 09-02-2010 at 01:20 PM.
 
Old 09-02-2010, 02:03 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,123

Rep: Reputation: 407Reputation: 407Reputation: 407Reputation: 407Reputation: 407
Quote:
Originally Posted by johnsfine View Post
The problem is that you have execution time assignments that are not in any function. (The function doesn't need to be main).

You can assign values to global variables as part of defining the variables as long you use the correct syntax for setting values as part of the definition.

Code:
#include <stdio.h>

struct test
{
   int i;
   int j;
}obj = { 5, 10 };
As you can see, the correct syntax for setting structure members as part of definition of an instance of the structure is a rather ugly syntax (the association of values to members is by sequence rather than name). But that is the syntax C provides.
C99, actually, provides quite nice syntax to initialize structs and not only - see the standard:

http://www.open-std.org/jtc1/sc22/wg...docs/n1124.pdf

, examples on page 76, page 130.

...

To the OP - you should have started from reading the above standard - it doesn't allow the code you published in the opening post.
 
Old 09-03-2010, 03:27 AM   #7
JohnGraham
Member
 
Registered: Oct 2009
Posts: 401

Rep: Reputation: 113Reputation: 113
Quote:
Originally Posted by Sergei Steshenko View Post
To the OP - you should have started from reading the above standard
Erm... I'd say that would be a bit over the top. Learning the language by reading the standard cover-to-cover (or even in sections as relevant) would be a waste of time and effort!
 
Old 09-03-2010, 06:41 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,123

Rep: Reputation: 407Reputation: 407Reputation: 407Reputation: 407Reputation: 407
Quote:
Originally Posted by JohnGraham View Post
Erm... I'd say that would be a bit over the top. Learning the language by reading the standard cover-to-cover (or even in sections as relevant) would be a waste of time and effort!
Just the opposite. The standard has a lot of example.

About 30 or years ago I caught myself tending to find answers in Pascal BNF diagram rather than in the book with it.
 
  


Reply

Tags
error, main, parse, structure


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP Error Parse error: parse error, unexpected $ in /home/content/S/k/i/SkinCare4U/h CowanServices Programming 2 12-09-2008 08:26 PM
Parse error: parse error, unexpected '}' in /home/content jouvert Linux - Newbie 3 05-17-2008 06:55 PM
Parse error: parse error, unexpected $ in /home/content/d/o/m/domain/html/addpuppy2.p Scooby-Doo Programming 3 10-25-2007 09:41 AM
xgettext: How to parse entire directory structure (dirs/subdirs) Muffe Linux - Software 1 04-28-2006 09:52 AM
error in accessing structure variable in kernel modules dypgrp Programming 3 01-17-2005 07:42 AM


All times are GMT -5. The time now is 02:55 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration