Thread: Question on typedef, structures

  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
    28,413
    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 08:02 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

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, 09: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, 03:21 AM
  5. Question on Structures in C - a different one
    By gvs in forum C Programming
    Replies: 5
    Last Post: 02-09-2005, 02:23 AM