int main (int argc, char *argv[]) { char **warray; size_t numleft; size_t temp; size_t lhold; size_t lstr=0;
numleft=(size_t)(argc-1); /* number of words to put in array */ warray=malloc(numleft * sizeof(*warray));
if (warray == NULL) { printf("can't allocate memory for warray\n"); exit(EXIT_FAILURE); } for (temp=0; temp < numleft; temp++) { /* get length of longest word */ lhold = strlen (argv[temp+1]); if (lhold > lstr) lstr = lhold; warray[temp] = argv[temp+1]; /* put argument into array */ } qsort(warray, lstr, numleft, strcmp);
for (lstr=0;lstr<numleft;lstr++) { printf("warray = %s\n",warray[lstr]); } return 0;
}
-- comp.lang.c.moderated - moderation address: c...@plethora.net -- you must have an appropriate newsgroups line in your header for your mail to be seen, or the newsgroup name in square brackets in the subject line. Sorry.
I suspect that you're missing a layer of indirection.
qsort() calls its comparison function with the ADDRESSES of two objects in the array. The array you have is an array of character pointers, so you're actually getting the addresses of those pointers passed to strcmp, which ought to be pretty much pure garbage.
-s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nos...@seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! -- comp.lang.c.moderated - moderation address: c...@plethora.net -- you must have an appropriate newsgroups line in your header for your mail to be seen, or the newsgroup name in square brackets in the subject line. Sorry.
> I suspect that you're missing a layer of indirection.
> qsort() calls its comparison function with the ADDRESSES of two objects > in the array. The array you have is an array of character pointers, > so you're actually getting the addresses of those pointers passed to > strcmp, which ought to be pretty much pure garbage.
Thank you. That and a small change made the program work.
Thanks again. -- comp.lang.c.moderated - moderation address: c...@plethora.net -- you must have an appropriate newsgroups line in your header for your mail to be seen, or the newsgroup name in square brackets in the subject line. Sorry.
> numleft=(size_t)(argc-1); /* number of words to put in array */
What do you think the cast accomplishes?
> warray=malloc(numleft * sizeof(*warray));
> if (warray == NULL) { > printf("can't allocate memory for warray\n"); > exit(EXIT_FAILURE); > } > for (temp=0; temp < numleft; temp++) { /* get length of longest word >*/ > lhold = strlen (argv[temp+1]); > if (lhold > lstr) > lstr = lhold; > warray[temp] = argv[temp+1]; /* put argument into array */ > } > qsort(warray, lstr, numleft, strcmp);
warray is an array of pointers, not an array of strings. qsort will pass the address of two array elements to strcmp. Therefore strcmp will not correctly process the addresses qsort passes it since it expects to receive pointers to strings, not pointers to pointers.
Your second argument is supposed to be the size of each element of warray. It isn't. It is the size of the longest string any element of warray points to and is completely unrelated to the size of the element itself.
-- Remove del for email -- comp.lang.c.moderated - moderation address: c...@plethora.net -- you must have an appropriate newsgroups line in your header for your mail to be seen, or the newsgroup name in square brackets in the subject line. Sorry.
In addition to what Seebs said, this call is defective, too. You've got the size and number of elements reversed and the size should just be the size of an array element (there's no need to compute lstr):
qsort(warray, numleft, sizeof *warray, mycomp); -- Larry Jones
You should see me when I lose in real life! -- Calvin -- comp.lang.c.moderated - moderation address: c...@plethora.net -- you must have an appropriate newsgroups line in your header for your mail to be seen, or the newsgroup name in square brackets in the subject line. Sorry.