Solved

casting with strcmp sort function

Posted on 2006-11-28
4
515 Views
Last Modified: 2008-01-09
Demonstrations of qsort which sort an array alphabetically usually define a sort function like this:

int compare(const void* A, const void* B) {
      return( strcmp(*((char **)A), *((char **)B) ) );
}

Why is there a need to cast to char**, and then derefence to get a char* ?  Why not just cast to char* ?

I tried:

int compare(const void* A, const void* B) {
      return( strcmp((char *)A), (char *)B) ) );
}

This doesn't sort properly.  But I can't see the difference between the two functions.  Can someone explain why casting to char** and then dereferencing is functionally different than simply casting to char* ?
0
Comment
Question by:chsalvia
[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
  • 3
4 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 125 total points
ID: 18027969
Casting only means re-interpreting an address.

1) So, casting to char** means re-interpreting the address as a pointer to pointer to char. De-referencing after that means looking for the data the pointer is pointing to, and treating that as a char*

2) Just casting to char* means re-interpreting the address as a pointer to char.

These are very different - they don't even refer to the same memory !

An example :

  int test = 5; /* value of test = 0x00000005 */
  int *ptest = &test; /* value of ptest = the address of test, say 0x12345678 */
  int **pptest = &ptest; /* value of pptest = the address of ptest, say 0x87654321 */

now :

1) cast to char** and de-reference :

      char **ppc = (char**) pptest; /* value of ppc = 0x87654321 */
      char *pc = *ppc; /* value of pc = 0x12345678 */
      /* the 4 first memory bytes pc is pointing to contains 0x00000005 */

2) cast to char* :

      char *pc = (char*) pptest; /* value of pc = 0x87654321 */
      /* the 4 first memory bytes pc is pointing to contains 0x12345678 */
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18027984
It's easier if you make a drawing of the above example, showing the pointers etc.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18028049
And about that compare function, you'd have to call it like this :

    int compare(const void* A, const void* B) {
         return( strcmp(*((char **)A), *((char **)B) ) );
    }

    char *strA = "stringA";
    char *strB = "stringB";
    compare(&strA, &strB);

Is that how it's done ?

If you want to use your compare function, you'd call it like this :

    int compare(const void* A, const void* B) {
         return( strcmp((char *)A, (char *)B ) );
    }

    char *strA = "stringA";
    char *strB = "stringB";
    compare(strA, strB);
0
 

Author Comment

by:chsalvia
ID: 18029583
>> And about that compare function, you'd have to call it like this :

Well, I wouldn't call it directly.  It is just meant to be passed to qsort.  I found the function in a qsort tutorial, in fact.

But I understand what you're saying about the different pointer locations.  Thank you.
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
How to design, and implement simple interface 5 139
Using ANSI C how to Read a .csv file 10 110
Test the speeds on my PC Drives 12 71
Windows Server 2012 R2 - connect to computer 13 62
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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