help with types needed

Discussion in 'C Programming' started by Ben, May 16, 2006.

  1. Ben

    Ben Guest

    Hi,

    I am trying to create a specific structure, but having problems with
    the type definitions.

    Basically it's an array of trees. Each element of the array should be a
    binary tree of type TREE.

    TREE is a pointer to a node TREENODE of the tree which containers
    pointers to the children.

    typedef struct TREENODE *TREE;
    struct TREENODE {
    USERID uid;
    TREE leftChild, rightChild;
    };

    /* Array of Tree pointers */
    typedef struct TREE USERBASE[100];

    The array is important since the index provides infomation about the
    what nodes are in the tree.

    The tree definition looks and seems to work fine, but my functions
    refer to a TREE by referring to an element of the ARRAY. I think this
    part may be breaking. The program compiles and runs fine, and it seems
    I can initialise elements of the array and add nodes, but the nodes
    don't appear to have been created when I try to extract info - all are
    NULL.

    What do I need to do to allow the array to properly point to a TREE or
    do I need to refer to the elements in a particular way (my functions
    don't use * or & etc.)?

    cheers
    Ben, May 16, 2006
    #1
    1. Advertising

  2. Ben

    Eric Guest

    Ben wrote:

    > Hi,
    >
    > I am trying to create a specific structure, but having problems with
    > the type definitions.
    >
    > Basically it's an array of trees. Each element of the array should be a
    > binary tree of type TREE.
    >
    > TREE is a pointer to a node TREENODE of the tree which containers
    > pointers to the children.
    >
    > typedef struct TREENODE *TREE;
    > struct TREENODE {
    > USERID uid;
    > TREE leftChild, rightChild;
    > };
    >
    > /* Array of Tree pointers */
    > typedef struct TREE USERBASE[100];
    >
    > The array is important since the index provides infomation about the
    > what nodes are in the tree.
    >
    > The tree definition looks and seems to work fine, but my functions
    > refer to a TREE by referring to an element of the ARRAY. I think this
    > part may be breaking. The program compiles and runs fine, and it seems
    > I can initialise elements of the array and add nodes, but the nodes
    > don't appear to have been created when I try to extract info - all are
    > NULL.
    >
    > What do I need to do to allow the array to properly point to a TREE or
    > do I need to refer to the elements in a particular way (my functions
    > don't use * or & etc.)?
    >
    > cheers

    Hmm, try this:
    typedef struct _TREENODE TREE;

    typedef struct _TREENODE{
    USERID uid;
    TREE *leftChild, *rightChild;
    }TREENODE;

    /* Array of Tree pointers */
    TREE USERBASE[100];
    Eric
    Eric, May 16, 2006
    #2
    1. Advertising

  3. Ben

    Jack Klein Guest

    On 15 May 2006 19:15:01 -0700, "Ben" <> wrote in
    comp.lang.c:

    > Hi,
    >
    > I am trying to create a specific structure, but having problems with
    > the type definitions.


    We're going to have problems helping you since you did not post real,
    compilable code.

    > Basically it's an array of trees. Each element of the array should be a
    > binary tree of type TREE.


    Your terminology is confused, probably helped along by the sort of
    typedefs that make C code hard to understand (and write).

    > TREE is a pointer to a node TREENODE of the tree which containers
    > pointers to the children.


    Generally speaking, typedef'ing pointers to object types in general is
    a big mistake. Others may disagree, and I am aware that some C
    programming books and classes do this a lot.

    > typedef struct TREENODE *TREE;


    Why are you using ALL UPPERCASE IDENTIFIERS for struct tags and
    typedef's? The best current practice is to reserve these for MACROs,
    and perhaps enumeration constants.

    > struct TREENODE {
    > USERID uid;
    > TREE leftChild, rightChild;
    > };
    >
    > /* Array of Tree pointers */
    > typedef struct TREE USERBASE[100];


    Here is where I know you are not posting real code. You can't typedef
    an array of struct TREE as anything, as TREE is not a struct of any
    type, but rather a pointer to a struct.

    > The array is important since the index provides infomation about the
    > what nodes are in the tree.
    >
    > The tree definition looks and seems to work fine, but my functions
    > refer to a TREE by referring to an element of the ARRAY. I think this


    What is the 'ARRAY'? Is ARRAY another typedef? Is it a macro?

    > part may be breaking. The program compiles and runs fine, and it seems
    > I can initialise elements of the array and add nodes, but the nodes
    > don't appear to have been created when I try to extract info - all are
    > NULL.


    How do you know you are initializing and adding nodes correctly, when
    you can't find the initial data after you put it there? Where is the
    memory coming from that is being assigned to the pointers?

    > What do I need to do to allow the array to properly point to a TREE or
    > do I need to refer to the elements in a particular way (my functions
    > don't use * or & etc.)?
    >
    > cheers


    First, you haven't posted real code, just some structure and type
    definitions that together would not actually compiler. Than you
    haven't shown us any of the code that actually uses (the real version)
    of these things.

    Perhaps your confusion is what happens in C when you pass an array to
    a function. Perhaps not, it is hard to tell with your retyped
    definitions and no code.

    The first thing to do is get rid of all the typedefs, and you will
    probably have a clearer idea of what is going on behind them. Try
    this:

    struct treenode
    {
    USERID uid;
    struct treenode *leftChild;
    struct treenode *rightChild;
    };

    Then skip the typedef for the pointer type completely, and when you
    want to define an array of 100 pointers to this struct, just do this:

    struct treenode *userbase[100];

    If you define an array of 100 pointers to a treenode structure, then
    there is no way you can add to it.

    Show some real code, but I'd strongly suggest getting rid of the
    typedef's first.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, May 16, 2006
    #3
  4. Ben

    Ben Guest

    Hmm, long answer if the question didn't make sense! Thanks for the
    attempt, I was purposely avoiding posting a mass of code, but the stuff
    I did include was "real".

    Turned out to be something probably quite obvious to regular C
    programmers.
    My type-ing was fine, but the functions were not pointing to the global
    array, hence they were taking a copy and operating on that - not doing
    anything to the global version.

    I just changed all the variables in my functions to pointers to the
    same variables and it works.

    I did change the array slightly too. You were right - the typedef was a
    bit unnecessary. The following doesn't require a typedef for the array:

    TREE userbase[MAXR];

    Anyway, the basic idea of an array of pointers to a "tree" (implemented
    as a linked list) worked fine.
    Ben, May 16, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sathyaish
    Replies:
    2
    Views:
    583
    Sathyaish
    May 22, 2005
  2. Soren Kuula
    Replies:
    2
    Views:
    546
    Henry S. Thompson
    Dec 1, 2005
  3. Jeremy Cowles

    STD types vs C++ intrinsic types

    Jeremy Cowles, Aug 18, 2003, in forum: C++
    Replies:
    5
    Views:
    1,867
    Bob Jacobs
    Aug 19, 2003
  4. Steve Knight
    Replies:
    2
    Views:
    745
    Steve Knight
    Oct 10, 2003
  5. Code4u
    Replies:
    6
    Views:
    297
Loading...

Share This Page