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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
typedef struct information
{
char name[11];
int age ;
}info_1 , *info_2 ;
info_1 member_1 ;
info_2 reference_1 ;
reference_1 = &member_1 ; /// why this line is not working
That's why he has to put this code in a function...
Code:
int main() // or something
{
...
}
@OP Why are you trying to make the pointer to the structure have the value of the structure? It already points to that value...
Code:
typedef struct Person
{
char name[11];
int age ;
} Person, *Person_1 /* why do you want this?*/;
Person member_1;
// Person_1 reference_1;
Person *reference_1= &member_1;
Alex Brinister
Last edited by abrinister; 08-23-2012 at 10:50 PM.
I got the same error after putting your statements as global. This is because, in global contexts you can initialise the global only once which is done along with the declaration itself.But you are using the 'global' (member_1 and reference_1) twice for initialising them.So, the error...If you don't initialise global during its declaration, they are by default initialised to NULL (in case of ptrs) or zero (in case of int/float/...). So, now to avoid the warnings and error you get, put the initialisation statement with the declrn of reference_1 as followed.
typedef struct information
{
char name[11];
int age;
}info_1, *info_2;
That is wrong syntax. If you are just typedefing a struct to a word, then you don't need the second person. In fact, you don't even need the first person. This would work.
Code:
typedef struct
{
char name[11];
int age;
}Person;
The 'Person' at the end specifies the identifier for the modified structure we just made. If you really wanted to, you could do
Code:
typdef struct Person
{
char name[11];
int age;
}person;
The Person at the beginning is not important but the one at the end is.
Typedefing a struct is just like creating a struct. You cannot do this:
So, you have quite a confusion between definition and declaration of structure..... and working of typedef.
The place where you used typedef indicates that "Person" is a datatype of type 'struct Person' (here Person is a struct with no contents). That's it, here you declared "person" as typedefd datatype of (struct person).....
And again when you tried for declaring "person" as a structure containing (char[] and an int), compiler stuck here saying that it "expects identifier or ‘(’ before ‘{’ token".
Because it is thinking that whatever that comes after the "Person" from now is every time a variable/identifier as the "Person" is previously typedefd as a datatype (like datatypes int , float, char, etc...)of (struct person). So your way of making things go wrong here....!!!Convinced?????????????
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.