LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-23-2012, 08:54 PM   #1
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Rep: Reputation: Disabled
typedef with pointers


Code:
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
 
Old 08-23-2012, 11:09 PM   #2
abrinister
Member
 
Registered: Dec 2010
Location: /home
Distribution: RL: Arch x64 VM: Debian
Posts: 460

Rep: Reputation: 38
What is the error you are getting? It compiles fine for me...

Alex Brinister
 
Old 08-23-2012, 11:15 PM   #3
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
error is :: ‘reference_1’ does not name a type
 
Old 08-23-2012, 11:19 PM   #4
abrinister
Member
 
Registered: Dec 2010
Location: /home
Distribution: RL: Arch x64 VM: Debian
Posts: 460

Rep: Reputation: 38
Does your program look something like this?

Code:
#include <stdio.h>

int main()
{
        typedef struct information
        {
                char name[11];
                int age;
        }info_1, *info_2;

        info_1 member_1;
        info_2 reference_1;

        reference_1 = &member_1;

        return 0;
}
I don't get any errors from this...

Is that your whole program? Is there more of it?

Alex Brinister
 
Old 08-23-2012, 11:27 PM   #5
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
please , put the whole code as global than you will find that error !
 
Old 08-23-2012, 11:40 PM   #6
abrinister
Member
 
Registered: Dec 2010
Location: /home
Distribution: RL: Arch x64 VM: Debian
Posts: 460

Rep: Reputation: 38
Code:
typedef struct information
{
        char name[11];
        int age;
}info_1, *info_2;

info_1 member_1;
info_2 reference_1;

&reference_1 = &member_1;
You have to make reference_1 a reference.

The only error I get with this is a required '(' before the & symbol...

Code:
test.c:10:1: error: expected identifier or '(' before '&' token
Alex Brinister
 
Old 08-23-2012, 11:44 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,640

Rep: Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118
You cannot have executable statements outside functions. Plus your names are really bad. Correction:

Code:
typedef struct Person
{
        char name[11];
        int age ;
} Person;

Person member_1;
Person *reference_1= &member_1;
 
1 members found this post helpful.
Old 08-23-2012, 11:46 PM   #8
abrinister
Member
 
Registered: Dec 2010
Location: /home
Distribution: RL: Arch x64 VM: Debian
Posts: 460

Rep: Reputation: 38
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 11:50 PM.
 
Old 08-24-2012, 12:06 AM   #9
SIG_SEGV
Member
 
Registered: Jul 2012
Location: Banglore, INDIA
Distribution: Fedora-Core
Posts: 70

Rep: Reputation: 11
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;

info_1 member_1;
info_2 reference_1 = &member_1;
/* reference_1 = &member_1; */
 
1 members found this post helpful.
Old 08-24-2012, 12:20 AM   #10
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
@ { Nevemteve & abrinister & SIG_SEGV } Sir , can you please tell me that

Code:
typedef struct Person person ;
person
{
          char name[11];
          int age ;
}
why this is wrong , because we have declared that our name is person , and now what is the use of , type in this scenario .
 
Old 08-24-2012, 12:28 AM   #11
abrinister
Member
 
Registered: Dec 2010
Location: /home
Distribution: RL: Arch x64 VM: Debian
Posts: 460

Rep: Reputation: 38
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:

Code:
struct Person;

Person
{
//stuff
}
Alex Brinister
 
Old 08-24-2012, 01:03 AM   #12
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,640

Rep: Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118
Pick one:

#1
Code:
typedef struct Person Person;

struct Person
{
          char name[11];
          int age ;
};
#2
Code:
struct Person
{
          char name[11];
          int age ;
};

typedef struct Person Person;
#3
Code:
typedef struct Person 
{
          char name[11];
          int age ;
} Person;
Note: don't use different names (person and Person): pick one and use that in every context.

Last edited by NevemTeve; 08-24-2012 at 01:05 AM.
 
Old 08-24-2012, 01:09 AM   #13
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
Code:
typedef struct Person Person;

Person
{
          char name[11];
          int age ;
};
, why this is wrong , any reason behind this !
 
Old 08-24-2012, 01:34 AM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,640

Rep: Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118Reputation: 1118
Because it doesn't follow C-syntax. (Perhaps you somehow confused typedef with #define. That's wrong.)
 
Old 08-24-2012, 02:08 AM   #15
SIG_SEGV
Member
 
Registered: Jul 2012
Location: Banglore, INDIA
Distribution: Fedora-Core
Posts: 70

Rep: Reputation: 11
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?????????????
 
1 members found this post helpful.
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
C typedef notation. corp1126 Programming 6 11-24-2009 11:40 AM
typedef in C Oaks Programming 1 08-05-2009 10:49 AM
[C] The FAR in typedef koyi Programming 2 07-21-2006 08:37 PM
Can you help me with typedef? brownstone Programming 3 06-11-2004 11:09 AM
sizeof my typedef... jhorvath Programming 7 10-11-2002 06:41 PM

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

All times are GMT -5. The time now is 04:53 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration