简体   繁体   中英

Returning a pointer to structure from a function

I have been trying to return a pointer to a structure from a function using the following code and a function which accepts a structure and returns a pointer to it :

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct mystruct
{
    int id;
    char name[10];
};


//this is the function
struct mystruct *return_pointer(struct mystruct a)
{
    struct mystruct *ptr;
    ptr = &a;
    return ptr;
}

int main()
{
    struct mystruct tag, *p;
    tag.id = 12;
    strcpy(tag.name,"abcd");

    printf("Values are: %d\t%s",tag.id,tag.name);
    p=return_pointer(tag);

    printf("\nValues through pointer: %d\t%s", p->id, p->name);
    return 0;
}

but when i try to access the structure's values using the returned pointer, its not working properly. It is only displaying the 'id' but not the 'name'. What can be the possible problem here? I have read in a book which said to use this in function's body:

ptr = (struct mystruct *)malloc(sizeof(struct mystruct));
ptr-> id = a.id; 
strcpy(p->name,a.name); 
//struct 'a' is argument to function

return ptr;

If this is the correct solution then why so?

Because you are returning the a that is a copy from the one you passed. Parameters in are passed by value, thus a is a copy of tag allocated in a different place, that place is the stack frame of the function and it is destroyed when the function returns.

So at the moment of printing you are printing the deallocated struct , and that is undefined behavior. If you want your code to work for whatever reason try this

struct mystruct *
return_pointer(struct mystruct *a)
{
    return a;
}

and in main() change it to

p = return_pointer(&tag);
//                 ^ Pass the address of tag and return it
//                   the same as
//                    
//                                   p = &tag;
//
//                   which is why they say it's pointless in
//                   the comments

When you use malloc() you allocate the struct on the heap, the data will be valid wherever accessible 1 until you manually destroy it with free() , the free() function will simply release the memory it doesn't care what will be done with it later, it just gives it back to where it came from.

Also, ALWAYS check the return value of malloc() .


1 Wherever there is a pointer holding the address of the memory originaly returned by malloc() . This is exactly the address that you MUST pass to free() when you decide that you don't need the struct anymore.

You should do this instead:

p = &tag;

To point to the same structure tag . Don't do this:

p = return_pointer(tag);

Because when you pass the tag , you pass it by value and it creates a copy (which is called a in the function return_pointer ) thus, your tag and a (specifically, the addresses of those (that is &tag and &a ) are different - See Mr. Caleb's comment) in the function is not the same.

Function parameters are the function's local variables. They are destroyed after the function finishes its work.

What you are trying to do is to pass a structure by reference and return the reference to it. In this case the function will look like

//this is the function
struct mystruct * return_pointer( struct mystruct *ptr )
{
    return ptr;
}

and called like

p = return_pointer( &tag );

Though there is no great usefulness of such a function. Nevertheless in general it could for example change some data members of the passed object.

I am a code-beginner from china,the error of your code is that the domain of local variable (ptr which is a pointer) is only effective in the function,but when your return it to your global main function,the memory which it point to have been freed. so it turn into a error. MY PLEASURE TO ANSWER YOUR !

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