Allegro.cc - Online Community

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

This thread is locked; no one can reply to it. rss feed Print
typedef struct{}*poly;
type568
Member #8,381
March 2007
avatar

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

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

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

Would you do please.. ?

X-G
Member #856
December 2000
avatar

... 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
avatar

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.

1flat **cube;
2 
3int 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 
15int 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
avatar

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<poly;i++)
6 array<i>=i;
7 
8 for(i=0;i<poly;i++)
9 for(j=i;j<poly;j++)
10 if(cube[array<i>].z<cube[array[j]].z)
11 {
12 array<i>+=array[j];
13 array[j]=array<i>-array[j];
14 array<i>-=array[j];
15 }
16 
17 for(i=0;i<poly;i++)
18 triangle(dest,cube[array<i>].cor[0][0],cube[array<i>].cor[0][1],cube[array<i>].cor[1][0],cube[array<i>].cor[1][1],cube[array<i>].cor[2][0],cube[array<i>].cor[2][1],cube[array<i>].col);

BAF
Member #2,981
December 2002
avatar

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
avatar

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: