Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   passing pointer->struct->pointer->struct to function. .. ?? (http://www.velocityreviews.com/forums/t436638-passing-pointer-struct-pointer-struct-to-function.html)

beetle 01-24-2005 07:51 PM

passing pointer->struct->pointer->struct to function. .. ??
 
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...!

Jens.Toerring@physik.fu-berlin.de 01-24-2005 08:26 PM

Re: passing pointer->struct->pointer->struct to function. .. ??
 
beetle <beetle@beetlemail.com> wrote:
> 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
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de

beetle 01-25-2005 06:08 PM

Re: passing pointer->struct->pointer->struct to function. .. ??
 
On 24 Jan 2005 20:26:26 GMT, Jens.Toerring@physik.fu-berlin.de wrote:

> beetle <beetle@beetlemail.com> wrote:
> > I'm storing data in several different binary tree's.
> > The root node is located in a struct containing general data about the
> > tree.


roger.. thnkx.... !

B


All times are GMT. The time now is 11:34 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.