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 8539095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:16:54+00:00 2026-06-11T11:16:54+00:00

To compare string using library function, i learnt to write a comparison function to

  • 0

To compare string using library function, i learnt to write a comparison function to compare two strings, but im not really clear about why are they doing this.

int StrCmp(const void *a, const void *b){
    char *s1 = *(char**) a;
    char *s2 = *(char**) b;
    return strcmp(s1,s2);
}

Why do they have to cast it to char ** first?
why not just cast them to char * directly?
like

return strcmp((char*)a, (char*)b);

Whats the meaning of casting ( a pointer) to ( a pointer to pointer)
If i have

char *x = "string";

if i do casting for x

(char**)x; // what is this? Is this character 's'?

Im quite confuse with this, thanks for clarify
and one more question is about the const
if it is a const, can i still cast them?(altought i know i can)

  • 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-11T11:16:55+00:00Added an answer on June 11, 2026 at 11:16 am

    “Why do they have to cast it to (char pointer pointer) first?
    why not just cast them to (char pointer) directly?”


    They have to cast it to what it is. Assuming the value
    coming in points to memory location 10,000, you have
    something like this:

      (char **) 
    
      a                 address a
                        "points to"
                        *a                **a   
      mem 1000          mem 10000         mem 20000
      to mem 1007       to mem 10007
      _____________     _____________     _______________ 
      pointer var  --> char pointer   ->  char
      10000             20000             'a'
      _____________     _____________     _______________
    

    If you cast it to a (char *) you are telling the compiler
    that the address referred to by a (in this case 10,000) is a char.
    Which is not correct.

      (char *)          address a points 
      a                 to
      mem 1000          mem 10000         mem 20000
      to mem 1007
      _____________     ______            ________________
      pointer var  -->  "char"            unreachable char
      10000             ?                 'a'
      _____________     ______            ________________
    

    “Whats the meaning of casting ( a pointer) to ( a pointer to pointer) If i have

    char *x = “string”;
    if i do casting for x

    (char**)x; // what is this? Is this character ‘s’?”


    What exists in this example is something like:

    x            *x or x[0] x[1]       x[2]      x[3]      x[4]      x[5]      x[6]
    mem 15000    mem 30000  mem 30001  mem 30002 mem 30003 mem 30004 mem 30005 mem 30006
    to 15007
    char *       char       char       char      char      char      char      char
    _________    _________  _________  _________ _________ _________ _________ _________
    
    30000    -->    's'       't'         'r'      'i'      'n'       'g'       '\0'
    ________     _________  _________  _________ _________ _________ _________ _________
    

    If you tell the compiler that x is a char** then it thinks this
    is the pattern:

    x              *x or x[0]         **x
                   char *
    mem 15000      mem 30000          "char" 
    to 15007       to 30007
    __________     _________          _________
    
    30000      --> "string\0?"
                   converted to
                   pointer value  ->   ??
    __________     _________          _________
    

    It incorrectly ends up going to whatever “address” the first 8 bytes starting at 30000 resolves to and getting a “character”. But since 30000 is the
    start of a null-terminated character array, at best it goes to some part of memory and gets some random byte, thinking it is a valid char. At worst it will get an address that is invalid for this program, causing a fatal error when it tries to access it.


    so can i just do

     return strcmp((char *)a, (char *)b);
    

    No, because a and b are not char pointers. To get char pointers you can’t avoid:

    return strcmp( *(char**)a, *(char**)b);
    

    Using Linden’s example you would call like so:

    #include <stdio.h>
    #include <string.h>
    
    int StrCmp(const void *a, const void *b){
        char *s1 = *(char**) a;
        char *s2 = *(char**) b;
        printf("s1:%s\n",s1);
        printf("s2:%s\n",s2);
        return strcmp(s1,s2);
    }
    
    const char* arr_of_ptr[] =
    {
      "hello",
      "world"
    };
    
    const char **p_arr_of_ptr = arr_of_ptr;
    
    int main(void)
    {
       const char *cstring1 = "LaDonna";
       const char *cstring2 = "McPherson";
       const char **pcstring1 = &cstring1;
       const char **pcstring2 = &cstring2;
       StrCmp(&arr_of_ptr[0],&arr_of_ptr[1]);
       StrCmp(pcstring1,pcstring2);
       StrCmp(p_arr_of_ptr,p_arr_of_ptr + 1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am comparing two strings, str1 and str2, using the string.compare function in #string.
I'm using two string vectors to store two text files. I need to compare
I'm using NUnit v2.5 to compare strings that contain composite Unicode characters. Although comparison
Given two strings text1 and text2 : public SOMEUSABLERETURNTYPE Compare(string text1, string text2) {
I would like to compare 4 character string using wildcards. For example: std::string wildcards[]=
I'm trying to compare a string stored in a structure. I'm using scanf to
I'm currently using this compare options to sort a list of strings: [self compare:aString
Are strings compared lexicographical when using the overriden bool operator<(const std::string & rhs) operator?
In my code I need to compare string letters but my problem is that
I want to compare an string which is in clear text in the database

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.