简体   繁体   中英

How can I return a pointer to an element inside an array of structs?

What am I doing wrong here ?

/*
 * Consider the following pseudo code !
 */
typedef struct foobar {
    unsigned char id, count;
    struct foobar *child;
} foobar;

foobar root = (foobar *) malloc( sizeof(struct foobar) );
root->child = (foobar *) malloc( sizeof(struct foobar) );

root->count++;
root->child[0].id = 1;

root->count++;
root->child[1].id = 2;

root->count++;
root->child[3].id = 3;

root->child[0].child = (foobar *) malloc( sizeof(struct foobar) );

root->child[0].child[0].count++;
root->child[0].child[0].id = 4;

root->child[1].child = (foobar *) malloc( sizeof(struct foobar) );
root->child[0].child[0].count++;
root->child[1].child[0].id = 5;

root->child[0].child[0].count++;
root->child[1].child[1].id = 6;

/* and so on */

/*
 * Function to search for an ID inside the tree,
 * it should call itself in order to go deeper into
 * the childs, but taht's not implemented here
 */
foobar *search( unsigned char id, foobar *start_node = NULL );
foobar *search( unsigned char id, foobar *start_node ) {
    if( start_node == NULL ) {
        unsigned char x;
        for( x = 0; x < root->count; x++ ) {
            if( root->child[ x ].id == id ) {
                foobar *ptr = &root->child[ x ];
                /* If I call ptr->id now, it will return the correct value */
                return &ptr;
            }
        }

    } else { /* not implemented */ }
}

/* Search the array for and ID */
foobar **ptr = this->search( 1 );
/* If I call ptr->id now, it will return memory garbage */

root has 4 children (as you access root->child[3]), so you must allocate enough memory:

root->child = (foobar *) malloc( sizeof(struct foobar) * 4 ); //at least 4

Also, you should return the foobar pointer itself, and not a pointer to it (ie return ptr; instead of return &ptr; .

You're returning the address of the pointer you've retrieved. You should be returning the pointer itself.

You only malloc memory for one child, but try to set the id for up to 4 children.

It should be like this:

root->child = (foobar *) malloc( sizeof(struct foobar) * 4 );

You are returning the address of a local variable from function search ( return &ptr; ). This object will be destroyed as soon as the search function is exited. Trying to use this memory location from outside the function will result in undefined behavior.

I was doing a couple things wrong.. in the code above the lines:

foobar *ptr = &root->child[ x ];
return &ptr;

Should be changed simply to return &root->child[ x ]; , this will return a pointer to the memory addr of root->child[ x ] .

The line foobar **ptr = this->search( 1 ); will become foobar *ptr = this->search( 1 ); , this will allow to access the struct properties using the . char; -> cannot be used and will output garbage. Correct usage example: (*ptr).description .

Many thanks to adamk !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM