help with types needed

B

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
 
E

Eric

Ben said:
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
 
J

Jack Klein

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.
 
B

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top