1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Structures, Pointers and typedefs

Discussion in 'C' started by vinsudhir, Jan 27, 2008.

  1. vinsudhir

    vinsudhir New Member

    Joined:
    Jan 27, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Wanted to know if this is possible and if so what it means.

    1 typedef struct tree{
    2. int height;
    3. int width;
    4. int weight;
    5. } banyan;

    6. int main(){

    7. banyan* count[2]; // This is the statement I am interested in

    8. count[0].height = 100;
    9. count[0].width = 10;

    10. return 0;

    }


    I do know about typedefs. For eg, we can make an instance of the type tree. but what is the meaning of statement 7? where we are instantiating something of type banyan, which is actually something of type tree. banyan is just an instance isnt it?

    The above program is just a sample. please let me know if something else has to be added for the above to make sense.

    thanks
     
  2. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    The typedef just creates an alias (a single word), for some other type. Some types in C can be fairly complicated and it gets tedious and error prone to keep writing them out in full.

    > banyan* count[2];
    Is no different to
    struct tree* count[2];

    banyan IS NOT a new type, nor an instance of a tree.

    A typedef doesn't create anything new, it's just a different way of referring to something else which already exists.
     
  3. vinsudhir

    vinsudhir New Member

    Joined:
    Jan 27, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    thanks for the comments.
     
  4. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA

    First write any code in CODE BLOCK as per looking good and easily readable.
     
  5. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    You can say " 'banyan' is synonym of 'struct tree' " ..
     
  6. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    always use for code block.

    Code:
    .....
    .....
    .....
    
    >>> For detail go to "code and guidlines" of this forum
     
  7. vinsudhir

    vinsudhir New Member

    Joined:
    Jan 27, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Ok. Thanks for the comments.
     

Share This Page