Thread: How to access members of a struct

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    55

    How to access members of a struct

    I have been trying to figure out how to access the members of a struct, but have been unsuccessful. The compiler is generating the error:

    "request for member `Element' in something not a structure or union"

    The code where this happens is:

    Code:
    if(  X < ( (T->Left)->Element )  )
    The struct is defined in a header file as:
    Code:
    struct AvlNode;
    typedef struct AvlNode *AvlTree;
    and in the implementation of that header as:
    Code:
    struct AvlNode
    {
     int Element;
     AvlTree*  Left;
     AvlTree*  Right;
     int Height;
    };
    I have declared an AvlTree object, as a global variable in main() and use extern in the implemenation of another of my header files.
    Code:
    AvlTree T;
    I would greatly appreciate it if someone could point me in the right direction. Thanks.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    So, if AvlTree is typedef'd to be a AvlNode* struct pointer, then:
    Code:
    struct AvlNode;
    typedef struct AvlNode *AvlTree;
    
    struct AvlNode
    {
     int Element;
     AvlTree*  Left;
     AvlTree*  Right;
     int Height;
    };
    ...is like saying:
    Code:
    struct AvlNode
    {
     int Element;
     struct AvlNode**  Left;
     struct AvlNode**  Right;
     int Height;
    };
    Notice the double **.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    Thanks hk_mp5kpdw, I changed

    Code:
    if(  X < ( (T->Left)->Element )  )
    to

    Code:
    if(  X < ( (*T->Left)->Element )  )

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    I am now having two problems that I cannot fix:

    Code:
    *T->Right = Insert( X, *T->Right );
    Crashes the program. When I change it to
    Code:
    T->Right = Insert( X, T->Right );
    Program works correctly but:

    1. Compiler generates warning:
    "passing argument 2 of insert from incompatible pointer type" and
    "assignment from incompatible pointer type"


    2. Program crashes in this part of the code:
    Code:
    if( Height( T->Right ) - Height( T->Left ) == 2 ) //Compiler gives above warning
     {  
        printf("Performing Right Rebalancing\n");
    
        if( X > ( (*T->Right)->Element) ) //Program crashes here.
             T = SingleRotateWithRight( T );
                 
     ...
    If I change that line to
    Code:
    if (X  > (T->Right)->Element);
    I am back at square one with the original problem I had.


    Is there a better way to structure this as to avoid the problems with the pointers?, or can this be fixed to work properly? Thanks.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Your use of typedef may be confusing matters here. What you are trying to do is make use of a binary tree structure. There should be no need for pointers to pointers that I see. If you really need this you will have something like **(T)->Right perhaps.


    Binary trees

    http://cslibrary.stanford.edu/110/BinaryTrees.html

    Not seeing the need to typedef your binary tree structure, simply use

    Code:
    struct AvlNode
    {
     int Element;
     int Height;
     AvlNode*  Left;
     AvlNode*  Right;
     int Height;
    };

    Pointer to pointers

    http://www.eskimo.com/~scs/cclass/int/sx8.html

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by slingerland3g View Post
    Your use of typedef may be confusing matters here.
    Indeed. A tree is a recursive structure, which means that a "tree" is identical to a "node." There is no need for a separate Tree type. All it is is a pointer to a Node.

    Constructs like (a->b)->c are poor. It implies that there is some other valid option, like a->(b->c). But the latter is not even syntactically correct. The parentheses are redundant. Just say a->b->c.

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Stole from another thread by matsp. This helps break down the use of pointer to pointer and its equivalent.

    a->b is short form of (*a).b. Since you have (*a)->b, we can rewrite that as (*(*a)).b, and remove the now unnecessary parenthesis: (**a).b.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    This is still all messed up because it's using a pointer to a pointer. Sure, you have a pointer-pointer, but who allocated the memory to store the pointer to which the pointer-pointer points?

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Quote Originally Posted by brewbuck View Post
    This is still all messed up because it's using a pointer to a pointer. Sure, you have a pointer-pointer, but who allocated the memory to store the pointer to which the pointer-pointer points?
    Can you say that 3 times really really fast!

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    Thanks for all of your suggestions. I went ahead and changed things to have a pointer to an avlNode struct and everything is working as it should. The code is also clearer and easier to understand. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  2. How to access struct fields in array
    By dv007 in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2006, 09:51 AM
  3. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM