C struct arrays
I have a C (not C++) struct that goes like this
typedef struct m开发者_开发百科ystruct{
float a,b;
int x, y;
} mystruct;
Then in a function I collect data like this:
mystruct List[MAX];
ListNumber = 0;
for(i = 0; i < MAX; i++)
{
if(conditions_meet)
{
List[ListNumber].a = masterlist[i].a;
...etc
ListNumber++;
}
}
then I send the array to a function
DoStuff(static int max, mystruct array[max]){
Stuff
}
This works, but when I try to do it like this....
mystruct setter(int i)
{
mystruct TEMP;
TEMP.a = masterlist[i].a;
//......etc
return TEMP;
}
mystruct List[MAX];
ListNumber = 0;
for(i = 0; i < MAX; i++)
{
if(conditions_meet)
{
List[ListNumber] = setter(i);
ListNumber++;
}
}
It causes a lot of funky errors. Why is this happening? edit: @tommieb75 I can't give much detail, the results do not seem to have a pattern. The list is used as a generalized way to draw stuff to the screen, and having the function instead of the direct setting makes odd problems in rendering -and random-, but produce no compiler errors at all. gdb shows some integers as being larger than an integer, that's the only pattern I find. masterlist is a global array of another struct. The data needs to be converted to the struct in this example. No compiler warnings or errors at all. I can turn in more sensitive warnings maybe, but I always get reported of any general error I can think. I am going to try the selected solution, that should suffice. Anyway similar functions returning structs are used in my code and all work perfectly except for this case with an array of structs.
For a simple setting a struct member you need a copy from an entire struct-element?
mystruct List[MAX];
ListNumber = 0;
for(i = 0; i < MAX; i++)
{
if(conditions_meet)
{
List[ListNumber].a = masterlist[i].a;
ListNumber++;
}
}
If you really need a function, use the destination-memory as parameter like:
void setter(mystruct *dest,const mystruct *src)
{
dest->a = src->a;
}
for(i = 0; i < MAX; i++)
{
if(conditions_meet)
{
setter( &List[ListNumber], &masterlist[i] );
ListNumber++;
}
}
what is
mystruct setter(i)
{
mystruct TEMP;
TEMP.a = masterlist[i].a;
'i' has any type?
//If you get errors with uninitialized members in struct that could help http://ideone.com/WRLVG
The first problem is your definition of setter is not a legal function signature. The parameter i
must be given a type
mystruct setter(int i) {
...
}
It also uses the variable masterlist
which is not defined in the function. This may be legally declared elsewhere as a static. If not though it will need to be accessible to the function in some way
The problem is that within the setter
function you have a stack allocated variable TEMP
which goes out of scope once the function returns... you might be better to allocate the pointer to my_struct
on the heap and return the address of it back out to the calling routine...
Edit:
mystruct *setter(int i){
mystruct *ptr_myStruct;
ptr_myStruct = malloc(sizeof(mystruct));
if (ptr_myStruct != NULL){
ptr_myStruct->a = masterlist[i].a
// etc...
return &ptr_myStruct;
}
return NULL;
}
mystruct List[MAX];
ListNumber = 0;
for(i = 0; i < MAX; i++)
{
if(conditions_meet)
{
List[ListNumber] = setter(i);
ListNumber++;
}
}
That is what is needed to get the values back out once the routine goes out of scope. That is called return-by-reference
精彩评论