Allegro.cc Forums » Programming Questions » typedef struct{}*poly;

 typedef struct{}*poly;
type568
Member #8,381
March 2007

I have gotten a problem, I want to swap structs, fast. (swap their places..)

 1 typedef float vect0r[4]; 2 3 typedef struct 4 { 5 vect0r cor[3]; 6 float z; 7 int col; 8 }flat; 9 10 flat *cube; 11 12 void some_function() 13 { 14 cube=(flat*)malloc(sizeof(flat)*poly); 15 16 17 for(i=0;i.z+=cube[j]; 22 cube[j]=cube-cube[j]; 23 cube-=cube[j]; 24 } 25 }

The sorting, of course returns an error... i tried various combinations of *, and & and again, of course none helped me.

 X-G Member #856 December 2000 ... why would + and - work on a struct without those operators defined? Hell, what do you even expect it to do in the first place?Copy them as usual. Use a temporary copy if you'd like. -- Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散！悪霊退散！怨霊、物の怪、困った時は　ドーマン！セーマン！ドーマン！セーマン！　直ぐに呼びましょう陰陽師レッツゴー！
kazzmir
Member #1,786
December 2001

First of all it looks like you are trying to sort these things so use qsort. Then don't make an array of structs, make an array of pointers to structs.

 1 flat **cube; 2 3 int compare_flat( void * a, void * b ){ 4 flat * a1 = (flat *) a; 5 flat * b1 = (flat *) b; 6 if ( a1->z < b1->z ){ 7 return -1; 8 } else if ( a1->z > b1->z ){ 9 return 1; 10 } else { 11 return 0; 12 } 13 } 14 15 int main(){ 16 cube = (flat **) malloc( sizeof(flat*) * 15 ); 17 for ( int i = 0; i < 15; i++ ){ 18 cube[ i ] = (flat *) malloc( sizeof(flat) ); 19 ...init.. 20 } 21 22 qsort( cube, sizeof(flat*), 15, compare_flat );

 Matt Smith Member #783 November 2000 Quote: Then don't make an array of structs, make an array of pointers to structs. No harm in using qsort on an array of structs, if the structs aren't enormous.
type568
Member #8,381
March 2007

Structs are small, but might be a huge amount...

Thanks kazzmir.

I expected to swap pointers.. While there are no pointers

EDIT: here how I made it though:

 1 2 int i,j; 3 int* array=(int*)malloc(sizeof(int)*poly); 4 5 for(i=0;i=i; 7 8 for(i=0;i].z+=array[j]; 13 array[j]=array-array[j]; 14 array-=array[j]; 15 } 16 17 for(i=0;i].cor[0][0],cube[array].cor[0][1],cube[array].cor[1][0],cube[array].cor[1][1],cube[array].cor[2][0],cube[array].cor[2][1],cube[array].col);

 BAF Member #2,981 December 2002 That is horrible. It's much easier, and better looking, if you just swap the elements the proper way, instead of using that extra array.
 type568 Member #8,381 March 2007 I dunno how much slower is it.. And probably the best way is **poly.. But it would be faster than swapping contents of the struct for sure.
 Go to: Allegro Development Installation, Setup & Configuration Allegro.cc Comments Off-Topic Ordeals The Depot Game Design & Concepts Programming Questions Recent Threads