passing pointer->struct->pointer->struct to function. .. ??

B

beetle

Hello,

I'm storing data in several different binary tree's.

The root node is located in a struct containing general data about the
tree.

struct lnode {
char *fname;
int nentry;

struct tnode *root;
};

struct tnode {
char *data;
int pos;

struct tnode *left, *right;
};

Now, how do I pass the binary tree root to a function since it's
located in another struct ?

For example this doesn't work :(
array[count]->root = tnode_insert(array[count]->root, data);

where array is a dynamic list of ptr's to lnode structs.

Ehmm.. actually I have another question now I am at it..
What's the best to init a struct containing different data-types to
zero ? Is it:
struct lnode *bla = {0};
or
struct lnode *bla = NULL;

Thnkx a lot...!
 
J

Jens.Toerring

beetle said:
I'm storing data in several different binary tree's.
The root node is located in a struct containing general data about the
tree.
struct lnode {
char *fname;
int nentry;
struct tnode *root;
};
struct tnode {
char *data;
int pos;
struct tnode *left, *right;
};
Now, how do I pass the binary tree root to a function since it's
located in another struct ?
For example this doesn't work :(
array[count]->root = tnode_insert(array[count]->root, data);
where array is a dynamic list of ptr's to lnode structs.

If 'array' is an array (dynamic or not dynamic) of structures (and not
an array of pointers to structures) and you want to pass the value of
the 'root' pointer element of the element numbered 'count' you need

array[ count ].root = tnode_insert( array[ count ].root, data );

You would use "array[ count ]->root" only if "array[ count ]" is a
pointer to a structure.
Ehmm.. actually I have another question now I am at it..
What's the best to init a struct containing different data-types to
zero ? Is it:
struct lnode *bla = {0};
or
struct lnode *bla = NULL;

Since you're not initializing a structure here but a pointer to a
structure the second alternative looks is correct. If you want to
initialize a structure you would probably use

struct lnode bla = { NULL, 0, NULL };

but

struct lnode bla = { 0 };

would also do.
Regards, Jens
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top