man qsort
==================================================
 
       void qsort(void *base, size_t nmemb, size_t size,
                  int(*compar)(const void *, const void *));
 
DESCRIPTION
       The qsort() function sorts an array with nmemb elements of size
size.  The base argument points to the start of the array.
 
       The  contents of the array are sorted in ascending order
according to a comparison function pointed to by compar, which is
called with two arguments
       that point to the objects being compared.
==================================================

as man says, "sorts an array with nmemb elements of size size."
my array contains MAX_NUM of (P*) 
> >         P** p = new P*[MAX_NUM];

but if it were 
int *i = new int[MAX_NUM]
and 
qsort(i, MAX_NUM, sizeof(i), CompInt);
works

am i doing something silly? can u pls try urself?



--- In [email protected], "Nico Heinze" <[EMAIL PROTECTED]> wrote:
>
> --- In [email protected], "Indika Bandara" <indikabandara19@>
> wrote:
> >
> > #define MAX_NUM 5
> >  
> > struct P
> > {
> >  
> >         P(){};
> >         P(int i){_i = i; };
> >         int _i;
> > };
> >  
> > int Comp(const void* p1, const void* p2)
> > {
> >         P* i1 = (P*)p1;
> >         P* i2 = (P*)p2;
> >         if(i1->_i>i2->_i)
> >                 return 1;
> >         if(i1->_i<i2->_i)
> >                 return -1;
> >  
> >         return 0;
> > }
> >  
> > int main(int argc, char** argv)
> > {
> >         P** p = new P*[MAX_NUM];
> >         p[0] = new P(1);
> >         p[1] = new P(9);
> >         p[2] = new P(8);
> >         p[3] = new P(3);
> >         p[4] = new P(2);
> >         printf("before\n");
> >         for(int i=0; i<MAX_NUM; i++)
> >                 printf("%d\n", p[i]->_i);
> >  
> >         qsort(p, MAX_NUM, sizeof(P*), Comp);
> 
> Shouldn't it be "sizeof( P)"?
> 
> >         printf("after\n");
> >         for(int i=0; i<MAX_NUM; i++)
> >                 printf("%d\n", p[i]->_i);
> >  
> > }
> <snip>
> 
> Regards,
> Nico
>


Reply via email to