ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
#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
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.
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:
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!
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.