Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7860583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:21:06+00:00 2026-06-02T22:21:06+00:00

This code snippet hand copied from a book I am reading: /* scmp: string

  • 0

This code snippet hand copied from a book I am reading:

/* scmp: string compare of *p1 and *p2 */
int scmp(const void *p1, const void *p2)
{
        char *v1, *v2;
        v1 = *(char **) p1; 
        v2 = *(char **) p2; 

        return strcmp(v1, v2);
}

This function is used with qsort to sort an array of strings. The point I don’t understand is, why v1 = *(char **) p1; instead of just v1 = (char *) p1; or wouldn’t even this work; v1 = p1;? I guess compiler should automatically typecast that assigment. Or even, consider this:

/* scmp: string compare of *p1 and *p2 */
int scmp(const void *p1, const void *p2)
{
        return strcmp(p1, p2);
}

I think (I might be awfully wrong) compiler is supposed to typecast p1 and p2 into char * since it’s what strcmp(char *, char *) expects.

To sum up, the question is why v1 = *(char **) p1 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:21:07+00:00Added an answer on June 2, 2026 at 10:21 pm

    qsort passes to the comparing function a pointer to the elements it has to compare; since in C there are no templates, this pointer is just brutally cast to a const void * (void * in C just means “this is some kind of pointer”, and to do something on it you must cast it back to its actual type).

    Now, if you are sorting an array of strings, each element you have to compare is a char *; but qsort passes to the comparison function a pointer to each element, so what your scmp receives is actually a char ** (a pointer to a pointer to the first character of the string), casted to a const void * because the signature of the comparison function says so.

    So, to get your char *, you have first to convert the parameters to their actual type (char **), and then dereference this pointer to get the actual char * you want to compare.

    (although, it would be more correct from a const-correctness point of view to cast to const char **)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

consider this code snippet void make(int n) { std::string user_input; std::istringstream iss(user_input); char letter;
hello everyone I have this snippet of the code: void* Init(int N) { Hand
This code snippet is from C# in Depth static bool AreReferencesEqual<T>(T first, T second)
hello everyone I have this snippet of the code: local fun NTimesF(f, n:int) =
This code snippet gets a Json data from a API hosted in my localhost.
This code snippet isn't working, I'm getting an Authentication Failed. response from the server.
See this code snippet int main() { unsigned int a = 1000; int b
Take this code snippet for example: window.location.href = 'mysite.htm#images'; Already being on the site
I found this code snippet here , but I just can't figure out what
I'm using this code snippet http://snipplr.com/view/43115/ and I wonder how I can access the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.