Re: Array of pointer,multiple object and struct question?

Discussion in 'C Programming' started by Ben Bacarisse, Dec 7, 2008.

  1. Eric <> writes:

    > typedef struct trn {
    > struct trn *right;
    > struct trn *left;
    > int key;
    > } treenode;
    > treenode *create_tree(void)

    > typedef struct m_t{
    > treenode *tree;
    > }tree_holder;
    > //***********************************
    > tree_holder *create_many_trees(int length){
    > treenode *a[length];
    > tree_holder *b;

    The array 'a' is a VLA (C99 only) and is, in fact, not needed.

    > ..some malloc code for b
    > ...some code to catch b==NULL

    More importantly, you need to malloc space for 'length' treenode
    pointers and to set b->tree to point to this space.

    > for(int i=0;i<length; i++){
    > a=create_tree();}

    Rather than set a, you should be setting b->tree directly.

    > tmp->tree=(tree_node_t*)a_t;

    We don't know what either or a_t is. POST REAL CODE (even in you edit
    it down). I suspect you mean b->tree = a; here. A local array like a
    disappears when the function returns, so putting a pointer to it in
    the returned result from the function won't work.

    > return (b);
    > }

    Ben Bacarisse, Dec 7, 2008
    1. Advertisements

  2. Eric <> writes:

    >> tmp->tree=(tree_node_t*)a_t;
    >>We don't know what either or a_t is.

    > Sorry for the typo. this line should be tmp->tree=(tree_node_t*)a;

    I don't think so. Your code had no tmp defined at that point (and no
    tree_node_t either). Please post actual code.

    >>A local array like a

    > disappears when the function returns, so putting a pointer to it in
    > the returned result from the function won't work.

    Your quoting is not working. I wrote all of the above but only the
    first line quoted as if it were from me.

    > I know

    If you know that the object named 'a' will vanish, the you should see
    that returning a pointer to it is a problem? You must not use a
    pointer to an object whose lifetime has ended.

    > (The tree holder: the job of tree holder is to hold multiple
    > trees).In main function there are the code:
    > ****Main function code*****
    > tree_holder *th;
    > int length
    > scanf( &length);
    > th=create_many_trees(length);
    > ************************

    Yes, I know. I did not comment on this part of your post because the
    main error is not here. The error is returning a pointer to struct
    that contains a pointer to an object that is about to disappear.
    (There is an error here as well -- you should check the return value
    from scanf but maybe you do in the real code.)

    > When user enters five, it creates five trees(inside create_many_tree
    > (int length)function, i have this"a=create_tree()".
    > After creating multiple tree, it return. So i can access it when i
    > insert data into first tree using
    > th->tree[0].
    > NOW My tree holder struct is the following
    > ********************************************
    > typedef struct m_t{
    > treenode *tree;
    > }tree_holder;
    > *******************************************
    > Of course it won't hold an array now because *tree is only a
    > treenode,not an array of treenode

    But tree could point to the start of an array of tree nodes. It is
    more likely that want it to point to an array of threenode pointers.
    See below...

    > But if i declared the struct tree_holder like this
    > ----------------------------------------
    > typedef struct m_t{
    > int length;
    > treenode *tree[length];
    > }tree_holder;
    > -----------------------------------------
    > The compiler will said length is undeclared.(the int length holds the
    > total number of tree after user enters it)
    > If i declare like this:
    > ****************************
    > typedef struct m_t{
    > treenode *tree[20];
    > }tree_holder;
    > ******************************
    > it works but my goal is to set number of tree after user enters it.

    You should then declare it as treenode **tree; Such a pointer can
    point to an array of any number of treenode pointers. You will have
    to consider how the function that will use this data get told how many
    pointers there are in the array, so you need to sort that out. The
    simplest method would be to store the size in the struct.

    > My main problem is how to set up the struct treeholder so that it can
    > hold multiple trees(the number of tree is not known until compile
    > time)

    I think you mean "until run time".

    > and the treeholder has to be able to hold multiple trees using
    > variable "tree". Also struct tree holder has to provide the ability to
    > access it when i insert data into the tree later (such as inserting
    > data in the first tree by calling th->tree[0] and pass it to an insert
    > fucntion.)

    The comment I made in my first reply still applies. Don't use a local
    array of treenode pointers -- malloc the space for them directly.

    Please, next time, _post real code_. There is very little difference
    between 'treenode *tree;' and 'treenode **tree;' on the page but all
    the world of difference in the meaning and anyone replying needs to be
    confident that you mean exactly what you wrote.

    Ben Bacarisse, Dec 8, 2008
    1. Advertisements

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. Chris Fogelklou
    Chris Fogelklou
    Apr 20, 2004
  2. beetle
    Jan 25, 2005
  3. Zero
    Barry Schwarz
    Nov 19, 2005
  4. erfan

    Array of pointer and pointer of array

    erfan, Jan 28, 2008, in forum: C Programming
    Martin Ambuhl
    Jan 28, 2008
  5. Replies:
  6. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    James Kuyper
    Sep 23, 2011
  7. Tuan  Bui
    it_says_BALLS_on_your forehead
    Jul 29, 2005
  8. aleksa

    Struct pointer vs. struct array pointer

    aleksa, Feb 20, 2013, in forum: C Programming
    Shao Miller
    Feb 20, 2013