469,927 Members | 1,888 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

help with types needed

Ben
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

May 16 '06 #1
3 1134
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

May 16 '06 #2
On 15 May 2006 19:15:01 -0700, "Ben" <be*********@gmail.com> 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/~a...FAQ-acllc.html
May 16 '06 #3
Ben
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.

May 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by GAR | last post: by
1 post views Thread by Robert Rota | last post: by
58 posts views Thread by jacob navia | last post: by
1 post views Thread by Shum | last post: by
159 posts views Thread by Bob Timpkinson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.