[Newbie] qsort question

  • Thread starter =?iso-8859-1?q?Alexander__D=FCnisch?=
  • Start date
?

=?iso-8859-1?q?Alexander__D=FCnisch?=

hi

I've written the follwing program:
=====================================
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int comparestr(const void *a, const void *b)
{
return strcmp((char*)a, (char*)b);
}

int main()
{
int len = 6;
char* names[] = {"Hansi", "Peter", "Ronny",
"Klaus", "Willy", "Michi"};
qsort(names, len, sizeof(char*), comparestr);
for(int i = 0; i < len; i++)
printf("%s ", names);
printf("\n");
return 0;
}
=====================================

but it doesn't seem to be sorting my array as I expected.
It prints:
Hansi Peter Ronny Klaus Willy Michi

What is my mistake?

Thanks for your help.
Best regards.
Alex
 
R

Richard Heathfield

Alexander Dünisch said:
hi

I've written the follwing program:
=====================================
but it doesn't seem to be sorting my array as I expected.
It prints:
Hansi Peter Ronny Klaus Willy Michi

What is my mistake?

You made several, I'm afraid, but the most important was that you forgot
that your comparison function accepts pointers to the objects stored in the
array, not the objects themselves. Since the objects that were stored are
pointers to char, the comparison function will get pointers to pointers to
char.

Here's a working version:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int comparestr(const void *vs1,
const void *vs2)
{
char *const *s1 = vs1;
char *const *s2 = vs2;

return strcmp(*s1, *s2);
}

int main(void)
{
size_t len = 6;
size_t i = 0;
const char *names[] = { "Hansi", "Peter", "Ronny",
"Klaus", "Willy", "Michi"
};

qsort(names, len, sizeof names[0], comparestr);
for(i = 0; i < len; i++)
printf("%s ", names);
printf("\n");
return 0;
}
 
K

Knemon

� said:
hi

I've written the follwing program:

[included in the modified program below]
but it doesn't seem to be sorting my array as I expected.
It prints:
Hansi Peter Ronny Klaus Willy Michi

What is my mistake?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int comparestr(const void *a, const void *b)
{
return strcmp((char *) a, (char *) b);
}

int replacement_comparestr(const void *a, const void *b)
{
return strcmp(*(char **) a, *(char **) b);
}

int main()
{
int len = 6;
char *names[] = { "Hansi", "Peter", "Ronny",
"Klaus", "Willy", "Michi"
};
printf("Attempt to sort with OP's comparestr\n");
qsort(names, len, sizeof(char *), comparestr);
for (int i = 0; i < len; i++)
printf("%s ", names);
printf("\n\n");

printf("Attempt to sort with replacement_comparestr\n");
qsort(names, len, sizeof(char *), replacement_comparestr);
for (int i = 0; i < len; i++)
printf("%s ", names);
printf("\n");

return 0;
}



Attempt to sort with OP's comparestr
Hansi Peter Ronny Klaus Willy Michi

Attempt to sort with replacement_comparestr
Hansi Klaus Michi Peter Ronny Willy
 
?

=?iso-8859-1?q?Alexander__D=FCnisch?=

int replacement_comparestr(const void *a, const void *b)
{
return strcmp(*(char **) a, *(char **) b);
}

Thanks a lot.
Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Qsort() messing with my entire Code 0
Qsort() is messing with my entire Code!!! 0
qsort man page 23
sorting an array of strings using qsort 1
Adding adressing of IPv6 to program 1
qsort 32
Fibonacci 0
calling qsort 59

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top