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

Discussion in 'C Programming' started by beetle, Jan 24, 2005.

  1. beetle

    beetle Guest

    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...!
    beetle, Jan 24, 2005
    #1
    1. Advertising

  2. beetle

    -berlin.de Guest

    beetle <> 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 ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Jan 24, 2005
    #2
    1. Advertising

  3. beetle

    beetle Guest

    On 24 Jan 2005 20:26:26 GMT, -berlin.de wrote:

    > beetle <> 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
    beetle, Jan 25, 2005
    #3
    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. James Vanns
    Replies:
    7
    Views:
    7,010
    Evan Carew
    Jan 21, 2004
  2. Ole
    Replies:
    4
    Views:
    585
    Michael Wojcik
    Oct 26, 2004
  3. Zero
    Replies:
    16
    Views:
    638
    Barry Schwarz
    Nov 19, 2005
  4. Vijai Kalyan
    Replies:
    4
    Views:
    689
    Vijai Kalyan
    Nov 8, 2005
  5. aleksa

    Struct pointer vs. struct array pointer

    aleksa, Feb 20, 2013, in forum: C Programming
    Replies:
    16
    Views:
    447
    Shao Miller
    Feb 20, 2013
Loading...

Share This Page