Solved

Using qsort()

Posted on 2006-07-01
8
361 Views
Last Modified: 2008-03-03
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,compare);
        printf("OK B\n");
                 }

The compiler keeps spitting out  OKA OKC OKC then  a segmentation fault(core dumped).

Any ideas??
0
Comment
Question by:Benjamin_Barrett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 

Author Comment

by:Benjamin_Barrett
ID: 17023830
I forgot to add that the segmentation fault suggests to me that its a pointer issue,  somewhere in compare() .....
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17023919
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
0
 

Author Comment

by:Benjamin_Barrett
ID: 17023932
Thanks for your quick response Paul

I tried your suggestion but I still get the same output.

I'll keep trying

Thanks

Benjamin
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Benjamin_Barrett
ID: 17024001
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?
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 500 total points
ID: 17024009
Hi Benjamin_Barrett,

I found it, and that's exactly what I would have suggested.

The problem is that:

char * s = "A String";

puts the string in protected memory and therefore the first attempt that qsort makes to swap two of the characters results in an access violation.

Your method makes the string into an array of bytes that is changeable.

Paul
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17024041
Hi Benjamin_Barrett,

FYI: To turn a char [] into a char*, just use &array[0].

Paul
0
 

Author Comment

by:Benjamin_Barrett
ID: 17024044
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
0
 

Author Comment

by:Benjamin_Barrett
ID: 17024407
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
undefined reference to `bswap_128' 9 186
Which version of C should I use when Perl is too slow? 13 187
Digital Output does not have continuity to ground? 3 105
What is sub-make ? 2 87
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question