Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returned Structure Reference Problem

Status
Not open for further replies.

fal762

Programmer
Aug 6, 2002
4
US
I thought this would be easy. All I wanted to do was to return a struct pointer instead of a char pointer from a function. The idea was that instead of listing numerous parameters when the function was called I would have all the returned values put into a structure and just the address of the structure returned to the calling procedure. It would then be up the the calling procedure to "unpack" the structure and use the various values.


The code below compiles and execures with VC++ 6 as a console application. However the returned values in the structure are not correct. I can usually figure out pointer problems but this one has me puzzled.



// begin code
#include <stdio.h>

struct chars_out { // structure template
char first;
char second;
int moredata;
float stillmoredata;
};


struct chars_out *test()
{
struct chars_out c; // pointer to instance of structure

char buf[8] = {'a','b','c','d','e','f','g'}; // test data

c.first = buf[0]; // test assignments
c.second = buf[1];
c.moredata = 10;

printf(&quot;Results of buff[0] are %c\n&quot;,c.first);
printf(&quot;Results of buff[1] are %c\n&quot;,c.second);
printf(&quot;address if c %d\n&quot;,&c);

return &c;

}



int main()
{

char *p;

struct chars_out *x;

x = test(); // invoke function

printf(&quot;Results of test are %d\n&quot;,x);

printf(&quot;Results of test are %d\n&quot;,x->first); // incorrect value ???
printf(&quot;Results of test are %d\n&quot;,x->second); // incorrect value ???

}
// end code
 
When I compiled your program on my Linux box, I got correct values. What values are you getting? //Daniel
 
You're returning a pointer to a local variable. Doing that will never produce correct results because when the stack unwinds as the function exits, the memory allocated on the stack for the structure is given back to the system. Whatever ends up there next ends up there. If nothing else ends up there, it can end up giving what looks like correct results sometimes, but it will never be consistent.

To sum it up: NEVER return a pointer to a local variable.
 
dd82 is right !

Do something like this:
Code:
void
test(struct chars_out *ptrStruct)
{
    struct chars_out c;     // pointer to instance of structure

    char buf[8] = {'a','b','c','d','e','f','g'};  // test data

     c.first = buf[0];         // test assignments
     c.second = buf[1];
    c.moredata = 10;

    printf(&quot;Results of buff[0] are %c\n&quot;,c.first);
    printf(&quot;Results of buff[1] are %c\n&quot;,c.second);
    printf(&quot;address if c %d\n&quot;,&c);
    
    *ptrStruct = c;
    return;  
}

main()
{
    struct chars_out x;

    test(&x);         // invoke function
    
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top