P
PCHOME
Hi!
I have questions about qsort( ). Is anyone be willing to help?
I use the following struct:
struct Struct_A{
double value;
...
} *AA, **pAA;
After allocating memory, I have pAA[n], and AA[n].
For all i, 0 <= i < n, pAA = &AA ;
I want to retrieve all the AA.value's according to the
value(decending order). So I want to sort.
Instead of sorting the whole AA[n](size_of(struct Struct_A) is very
large),
I use pAA as a handle and sort pAA according to pAA->value.
pAA[0]->value should be the biggest one.
I triied:
qsort( (void **) pAA, n, sizeof(struct Struct_A * ), comp);
and
int comp(const void * row1, const void * row2)
struct Struct_A * Arow1
= (struct Struct_A *) row1;
struct Struct_A * Arow2
= (struct Struct_A *) row2;
if ( Arow1->value < Arow2->value) return (1);
elseif if if ( Arow1->value > Arow2->value) return (-1);
else return(0);
}
The code can run, but not in the way I need.
before sort:
pAA[0]->value = 0.688875
pAA[1]->value = 0.580136
pAA[2]->value = 0.379049
pAA[3]->value = 0.606478
pAA[4]->value = 0.742888
pAA[5]->value = 0.908687
pAA[6]->value = 0.969579
after sort
pAA[0]->value = 0.969579
pAA[1]->value = 0.908687
pAA[2]->value = 0.742888
pAA[3]->value = 0.606478
pAA[4]->value = 0.688875
pAA[5]->value = 0.379049
pAA[6]->value = 0.580136
and the following one works.
int comp(const void * row1, const void * row2)
{
struct Struct_A * Arow1
= *((struct Struct_A **) row1);
struct Struct_A * Arow2
= *((struct Struct_A **) row2);
if ( Arow1->value < Arow2->value) return (1);
else if ( Arow1->value > Arow2->value) return (-1);
else return 0;
}
Why does "*((struct Struct_A **) row1)" work? but not "(struct Struct_A
*) row1"?
To me, Both row1 point to the same location "&AA".
I am confused why the (struct Struct_A *) row1 version can not lead to
the correct result?
Is anyone able to use PLAIN English explain what is the difference?
and why dose the 2nd can not sort by "value"?
A sorting need to swap data.
The first one swap pAA and pAA[j] if necessary.
What data will the (struct Struct_A *) row1 version indeed swap?
Thanks!
I have questions about qsort( ). Is anyone be willing to help?
I use the following struct:
struct Struct_A{
double value;
...
} *AA, **pAA;
After allocating memory, I have pAA[n], and AA[n].
For all i, 0 <= i < n, pAA = &AA ;
I want to retrieve all the AA.value's according to the
value(decending order). So I want to sort.
Instead of sorting the whole AA[n](size_of(struct Struct_A) is very
large),
I use pAA as a handle and sort pAA according to pAA->value.
pAA[0]->value should be the biggest one.
I triied:
qsort( (void **) pAA, n, sizeof(struct Struct_A * ), comp);
and
int comp(const void * row1, const void * row2)
struct Struct_A * Arow1
= (struct Struct_A *) row1;
struct Struct_A * Arow2
= (struct Struct_A *) row2;
if ( Arow1->value < Arow2->value) return (1);
elseif if if ( Arow1->value > Arow2->value) return (-1);
else return(0);
}
The code can run, but not in the way I need.
before sort:
pAA[0]->value = 0.688875
pAA[1]->value = 0.580136
pAA[2]->value = 0.379049
pAA[3]->value = 0.606478
pAA[4]->value = 0.742888
pAA[5]->value = 0.908687
pAA[6]->value = 0.969579
after sort
pAA[0]->value = 0.969579
pAA[1]->value = 0.908687
pAA[2]->value = 0.742888
pAA[3]->value = 0.606478
pAA[4]->value = 0.688875
pAA[5]->value = 0.379049
pAA[6]->value = 0.580136
and the following one works.
int comp(const void * row1, const void * row2)
{
struct Struct_A * Arow1
= *((struct Struct_A **) row1);
struct Struct_A * Arow2
= *((struct Struct_A **) row2);
if ( Arow1->value < Arow2->value) return (1);
else if ( Arow1->value > Arow2->value) return (-1);
else return 0;
}
Why does "*((struct Struct_A **) row1)" work? but not "(struct Struct_A
*) row1"?
To me, Both row1 point to the same location "&AA".
I am confused why the (struct Struct_A *) row1 version can not lead to
the correct result?
Is anyone able to use PLAIN English explain what is the difference?
and why dose the 2nd can not sort by "value"?
A sorting need to swap data.
The first one swap pAA and pAA[j] if necessary.
What data will the (struct Struct_A *) row1 version indeed swap?
Thanks!