LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   typedef with pointers (https://www.linuxquestions.org/questions/programming-9/typedef-with-pointers-4175423697/)

tushar_pandey 08-23-2012 07:54 PM

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


abrinister 08-23-2012 10:09 PM

What is the error you are getting? It compiles fine for me...

Alex Brinister

tushar_pandey 08-23-2012 10:15 PM

error is :: ‘reference_1’ does not name a type

abrinister 08-23-2012 10:19 PM

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

tushar_pandey 08-23-2012 10:27 PM

please , put the whole code as global than you will find that error !

abrinister 08-23-2012 10:40 PM

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

NevemTeve 08-23-2012 10:44 PM

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;


abrinister 08-23-2012 10:46 PM

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

SIG_SEGV 08-23-2012 11:06 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;

info_1 member_1;
info_2 reference_1 = &member_1;
/* reference_1 = &member_1; */

tushar_pandey 08-23-2012 11:20 PM

@ { 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 .

abrinister 08-23-2012 11:28 PM

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

NevemTeve 08-24-2012 12:03 AM

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.

tushar_pandey 08-24-2012 12:09 AM

Code:

typedef struct Person Person;

Person
{
          char name[11];
          int age ;
};

, why this is wrong , any reason behind this !

NevemTeve 08-24-2012 12:34 AM

Because it doesn't follow C-syntax. (Perhaps you somehow confused typedef with #define. That's wrong.)

SIG_SEGV 08-24-2012 01:08 AM

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?????????????


All times are GMT -5. The time now is 02:19 AM.