Benjamin_Barrett
asked on
Using qsort()
Hi
I'm having trouble using qsort(). I've tried for a while now & just cant master it.
My code (I've left my debugs in)
int compare( const void *a, const void *b)
{
printf("OK C\n");
return *(char*)a - *(char*)b;
}
void sorting()
{
char *a="string";
printf("OK A\n");
qsort(a,strlen(a),1,compar e);
printf("OK B\n");
}
The compiler keeps spitting out OKA OKC OKC then a segmentation fault(core dumped).
Any ideas??
I'm having trouble using qsort(). I've tried for a while now & just cant master it.
My code (I've left my debugs in)
int compare( const void *a, const void *b)
{
printf("OK C\n");
return *(char*)a - *(char*)b;
}
void sorting()
{
char *a="string";
printf("OK A\n");
qsort(a,strlen(a),1,compar
printf("OK B\n");
}
The compiler keeps spitting out OKA OKC OKC then a segmentation fault(core dumped).
Any ideas??
Hi Benjamin_Barrett,
Just in case, you may like to try:
return *((char*)a) - *((char*)b);
I cant see anything else wrong. I'll try it.
Paul
Just in case, you may like to try:
return *((char*)a) - *((char*)b);
I cant see anything else wrong. I'll try it.
Paul
ASKER
Thanks for your quick response Paul
I tried your suggestion but I still get the same output.
I'll keep trying
Thanks
Benjamin
I tried your suggestion but I still get the same output.
I'll keep trying
Thanks
Benjamin
ASKER
Well I tried changing the declaration & intialization of a(the string to sort) to....
char a[10] = "string";
instead of
char *a="string";
And it works!
Clearly this is a pointer issue...Im a Java programmer trying to learn better C, so pointers are a bit heavy going for me.
Any advice on why this worked & how to get it working with a pointer as an argument?
char a[10] = "string";
instead of
char *a="string";
And it works!
Clearly this is a pointer issue...Im a Java programmer trying to learn better C, so pointers are a bit heavy going for me.
Any advice on why this worked & how to get it working with a pointer as an argument?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Benjamin_Barrett,
FYI: To turn a char [] into a char*, just use &array[0].
Paul
FYI: To turn a char [] into a char*, just use &array[0].
Paul
ASKER
Excellent, thats why "OK C" was outputted twice before the fault
Ok, can you help me abit more....
If I have a pointer as an arg, how do I convert it to a non pointer?
I've tried doing declaring a string then making it equal the pointer...
char *a ="test";
char b[5] = a; || char b[5] = &a;
but no dice.
Any suggestions?
Just to let you know Im disappearing for lunch now, so no stress.
Benjamin
Ok, can you help me abit more....
If I have a pointer as an arg, how do I convert it to a non pointer?
I've tried doing declaring a string then making it equal the pointer...
char *a ="test";
char b[5] = a; || char b[5] = &a;
but no dice.
Any suggestions?
Just to let you know Im disappearing for lunch now, so no stress.
Benjamin
ASKER
Brilliant Paul!
You only need to access the address of the first element of the array to get the whole thing....
Thank you kindly for your help.
Benjamin
PS We posted @ the same time before, so I just got your last reply
You only need to access the address of the first element of the array to get the whole thing....
Thank you kindly for your help.
Benjamin
PS We posted @ the same time before, so I just got your last reply
ASKER