I have a C (not C++) struct that goes like this
typedef struct mystruct{
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.
A structure may contain elements of different data types – int, char, float, double, etc. It may also contain an array as its member. Such an array is called an array within a structure.
Array elements are accessed using the Subscript variable, Similarly Structure members are accessed using dot [.] operator. Structure written inside another structure is called as nesting of two structures. Nested Structures are allowed in C Programming Language.
An array of structure in C programming is a collection of different datatype variables, grouped together under a single name. General form of structure declaration. The structural declaration is as follows − struct tagname{ datatype member1; datatype member2; datatype member n; };
Structure can be defined as a data structure used as container which can hold variables of different types. On other hand Array is a type of data structure used as container which can hold variables of same type and do not support multiple data type variables.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With