Structure and typedef

This is a discussion on Structure and typedef within the C Programming forums, part of the General Programming Boards category; Have a basic question about strcture declaration and typedef As per my understanding, when I say int i,j,k; compiler set ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    Structure and typedef

    Have a basic question about strcture declaration and typedef

    As per my understanding, when I say
    int i,j,k;

    compiler set aside the memory for these. But how about this

    typedef int a;

    Will compiler set the memory for variable?

    Similarly, when I compile following program I do not get error

    Code:
    /*typedef*/ struct  // node 
    {
    	char a[100];
    	char node;
    }node;
    
    //node node1,node2;
    typedef int k;
    k b;
    
    main()
    {
    	printf("Hello\n");
    	printf("%d\n",node.a);
    //	printf("%d\n",node1.a);
    //	printf("%d\n",node2.a);
    }
    Moment I include typedef, I get error.

    Please describe, how this works?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    typedef int a;
    
    Will compiler set the memory for variable?
    What variable?
    Thats just a type declaration.
    It means treat a as another name for int.

    Moment I include typedef, I get error.
    Because your typedef and the struct tag have the same name.

    typedef struct structtag { } alias;

    structtag and alias cannot be the same.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When you typedef a variable, you are defining a new type. This does not create an instance of it. Watch:
    Code:
    struct
    {
        ... stuff here ...
    } instance;
    This defines a nameless structure, and creates an instance of it, called 'instance'.

    You cannot individually define instances of this structure now, because it has no name.
    Code:
    struct mystruct
    {
        ... stuff here ...
    } instance;
    
    struct mystruct another_instance;
    This defines a names structure (mystruct) and also creates an instance. We then create a second instance since it is named.
    Code:
    typedef struct
    {
        ... stuff here ...
    } node;
    
    node instance;
    Define a new data type named 'node' from a nameless structure. We then have to seperately define an instance of it.
    Code:
    typedef struct named
    {
        ... stuff here ...
    } node;
    
    node instance;
    struct named anotherinstance;
    As per above, except we have created instances two different ways.

    Key point here: the tag on the end of a typedef is the name of the data type, not the name of the instance of the variable.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Stoned_Coder
    Because your typedef and the struct tag have the same name.

    typedef struct structtag { } alias;

    structtag and alias cannot be the same.
    No. The problem is actually that they were thinking the alias was actually an instance of the variable. Look at their code.

    Note to original poster: This is what happens when you don't tell us the exact error you get. It pays to be specific.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    >>structtag and alias cannot be the same

    Following code works fine eventhough structtag and alias same.

    I feel is it typedef keyword that will not allow compiler to set aside the memory for struct and referecing member of such strcuture gives error.

    /*typedef*/ struct node
    {
    char a[100];
    char node;
    }node;

    main()
    {
    printf("%d\n",node.a);
    }

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    I agree

    Thanks

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Roaring_Tiger
    I feel is it typedef keyword that will not allow compiler to set aside the memory for struct and referecing member of such strcuture gives error.
    This is correct. The final parameter of a typedef takes the place of where you could normally create an instance (following the closing } of a structure. Using typedef removes this ability. You have to create your instances later. With a typedef, the final argument is the name of the new data type, not an instance of it.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SCSI read disc structure C++ help
    By Witchfinder in forum C++ Programming
    Replies: 0
    Last Post: 03-25-2009, 04:24 PM
  2. Structure - Typedef accessing problem
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 08-11-2006, 11:52 PM
  3. Replies: 5
    Last Post: 02-14-2006, 08:04 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. structure & typedef
    By beely in forum C Programming
    Replies: 9
    Last Post: 10-30-2002, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21