C structural arrays

I have a C structure (not C ++) that looks like this

typedef struct mystruct{ float a,b; int x, y; } mystruct; 

Then in the function, I collect the data as follows:

 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 an array of function

  DoStuff(static int max, mystruct array[max]){ Stuff } 

It 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++; } } 

This causes a lot of funky bugs. Why is this happening? edit: @ tommieb75 I cannot give a lot of details, the results do not seem to have a template. The list is used as a generalized way to draw material on the screen, and the function, instead of directly setting it, makes odd problems with rendering and random order, but does not create compiler errors at all. gdb shows that integers are greater than an integer, which is the only pattern that I find. masterlist is a global array of a different structure. In this example, the data must be converted to a structure. No warnings or compiler errors at all. Perhaps I can include more sensitive warnings, but I am always informed of any common error that I can think of. I am going to try the chosen solution, which should be enough. In any case, similar functions that return structures are used in my code, and everything works fine, except in this case with an array of structures.

+6
c arrays pointers
source share
4 answers

For a simple installation of a struct element, do you need a copy from the entire structural 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 a parameter, for example:

 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++; } } 
+1
source share

what

 mystruct setter(i) { mystruct TEMP; TEMP.a = masterlist[i].a; 

Is 'i' of any type?

// If you get errors with uninitialized members in the structure that could help http://ideone.com/WRLVG

+1
source share

The first problem is that your definition of a setter is not a signature of a legal function. Parameter i must be set to type

 mystruct setter(int i) { ... } 

It also uses the masterlist variable, which is not defined in the function. It may be legally declared elsewhere as static. If not, although it should be available for the function in some way

0
source share

The problem is that in the setter function you have the TEMP variable allocated by the stack, which goes out of scope after the function returns ... you might be better off highlighting the pointer to my_struct on the heap and return its address back to the calling procedure ...

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++; } } 

This is what is needed in order to return values ​​after the program goes beyond the bounds. This is called return-by-reference

0
source share

All Articles