pass 2 dimensional array to sort func - segmentation fault?

D

drjon

Any one know whats wrong with this code?

output
---------
pears
bananas
sort::apples
sort::pears
sort::bananas
segmentation fault

code
------
#include <string.h>
#include <stdio.h>

int
string_cmp (const void *p1, const void *p2)
{
return strcmp (*(char *const *) p1, *(char *const *) p2);
}

void
sort (char s[][40], unsigned cnt)
{
int i=0;
for (i=0;i<3;i++) printf("sort::%s\n", s);
qsort (s, cnt, sizeof (char *), string_cmp);
}

main()
{
char fruit[3][40]={"apples","pears","bananas" };
unsigned i=0;
for (i=0;i<3;i++) printf("%s\n", fruit);
sort(fruit,3);
for (i=0;i<3;i++) printf("%s\n", fruit);
}
 
I

Ike Naar

Any one know whats wrong with this code?

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

#include said:
int
string_cmp (const void *p1, const void *p2)
{
return strcmp (*(char *const *) p1, *(char *const *) p2);

return strcmp(p1, p2); /* see [0] */
}

void
sort (char s[][40], unsigned cnt)
{
int i=0;
for (i=0;i<3;i++) printf("sort::%s\n", s);
qsort (s, cnt, sizeof (char *), string_cmp);


qsort(s, cnt, sizeof *s, string_cmp); /* see [0] */

int main(void)
{
char fruit[3][40]={"apples","pears","bananas" };
unsigned i=0;
for (i=0;i<3;i++) printf("%s\n", fruit);
sort(fruit,3);
for (i=0;i<3;i++) printf("%s\n", fruit);


return 0; /* mandatory for C90 compiler, optional for C99 compiler */

[0] fruit is a two-dimension array, every element of fruit
is a one-dimensional array of char, not a pointer to char.
 
D

drjon

Thank you Ike Naar thats brilliant.

So my problem is thinking I can use char* when its really a char
array.

Also I see I can use
return strcmp( (const char *)p1, (const char *)p2);
in place of
return strcmp(p1, p2);

If you dont mind me asking : is sort(char s[][40], unsigned cnt)
the only way to pass the 2-dimensional array? Do I need to specify
[40] and cnt (ie 3) ?

Many thanks.
 
I

Ike Naar

So my problem is thinking I can use char* when its really a char
array.
Also I see I can use
return strcmp( (const char *)p1, (const char *)p2);
in place of
return strcmp(p1, p2);

Yes, but why? The casts are unnecessary.
If you dont mind me asking : is sort(char s[][40], unsigned cnt)
the only way to pass the 2-dimensional array? Do I need to specify
[40] and cnt (ie 3) ?

Yes.

But you could have declared the fruit array differently, e.g.
as an array of pointer to char:

char *fruit[] = {"apples","pears","bananas"};

In that case, you'd pass the array to your sort function using
a different parameter mechanism: sort(char *s[], unsigned cnt)
or, equivalently, sort(char **s, unsigned cnt) .
Passing the '40' would no longer be necessary, but you'd still have
to pass the number of array elements via the cnt parameter:

sort(fruit, 3);
sort(fruit, sizeof fruit / sizeof fruit[0]);

Also the compare function for qsort would be different (it would be
the compare function you provided in your first post).


For a better explanation on this subject, see section 6 of
the comp.lang.c FAQ at http://c-faq.com/aryptr/index.html .
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top