Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 212
Lincoln Avatar asked Jul 20 '26 05:07

Lincoln


2 Answers

Because you are returning the a that is a copy from the one you passed. Parameters in c 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 accessible1 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().


1Wherever 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.

like image 66
Iharob Al Asimi Avatar answered Jul 22 '26 19:07

Iharob Al Asimi


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.

like image 45
Ian Avatar answered Jul 22 '26 20:07

Ian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!