Question on typedef, structures

This is a discussion on Question on typedef, structures within the C++ Programming forums, part of the General Programming Boards category; Is this possible? if so what does it mean? Code: 1 typedef struct tree{ 2. int height; 3. int width; ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    1

    Question on typedef, structures

    Is this possible? if so what does it mean?

    Code:
    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 because of statement 1 (typedef struct tree).
    but what is the meaning of statement 7? where we are instantiating something of type banyan, which is an instance 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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    I think it would be clearer for you if this:
    Code:
    typedef struct tree{
        int height;
        int width;
        int weight;
    } banyan;
    Was re-written as:
    Code:
    struct tree {
        int height;
        int width;
        int weight;
    };
    
    typedef tree banyan;
    EDIT:
    By the way, you might want to remove line numbers when posting. Post code that is well indented, and if possible, compilable.
    Last edited by laserlight; 01-27-2008 at 07:02 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Typedeffing struct names is not necessary in C++ (unlike C where you'd have to use the struct keyword everywhere you declare instances, unless the struct is typedeffed).

    Line 7 creates an array for two pointers to this struct, but as long these are not set to point to anything, you cannot use them.

    You probably meant to declare an array of two structs (not pointers to struct):
    Code:
    7. banyan count[2]; // This is the statement I am interested in
    
    8. count[0].height = 100;
    9. count[0].width = 10;
    In C++ you could simply declare the struct as
    Code:
    1. struct banyan{
    2. int height;
    3. int width;
    4. int weight;
    5. };
    and line 7 would work just the same.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Maybe check your other forums for replies?
    Did you even bother to read it there before coming here?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error: redefinition of typedef xxxx
    By Rocha in forum C Programming
    Replies: 2
    Last Post: 11-24-2008, 08:19 AM
  2. Structures and pointers question
    By cjohnman in forum C Programming
    Replies: 2
    Last Post: 04-30-2008, 01:48 PM
  3. Question regarding typedef
    By esmeco in forum C Programming
    Replies: 2
    Last Post: 04-24-2008, 11:58 AM
  4. Newbie question on Structures
    By Gatt9 in forum C++ Programming
    Replies: 15
    Last Post: 03-26-2005, 02:21 AM
  5. Question on Structures in C - a different one
    By gvs in forum C Programming
    Replies: 5
    Last Post: 02-09-2005, 01:23 AM

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