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("Results of buff[0] are %c\n",c.first);
printf("Results of buff[1] are %c\n",c.second);
printf("address if c %d\n",&c);
return &c;
}
int main()
{
char *p;
struct chars_out *x;
x = test(); // invoke function
printf("Results of test are %d\n",x);
printf("Results of test are %d\n",x->first); // incorrect value ???
printf("Results of test are %d\n",x->second); // incorrect value ???
}
// end code
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("Results of buff[0] are %c\n",c.first);
printf("Results of buff[1] are %c\n",c.second);
printf("address if c %d\n",&c);
return &c;
}
int main()
{
char *p;
struct chars_out *x;
x = test(); // invoke function
printf("Results of test are %d\n",x);
printf("Results of test are %d\n",x->first); // incorrect value ???
printf("Results of test are %d\n",x->second); // incorrect value ???
}
// end code