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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:55:41+00:00 2026-05-24T15:55:41+00:00

I’m qsort()ing some memory pointers in a list to allow a bsearch of them

  • 0

I’m qsort()ing some memory pointers in a list to allow a bsearch of them later in a function. My question is, do I need to type cast those values to something other than const void * to do a legal comparison in C? I could just do the conversion and let the compiler tell me, but I have a feeling that this might be compiler dependent.

  • 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-05-24T15:55:43+00:00Added an answer on May 24, 2026 at 3:55 pm

    Comparisons are fine, arithmetic and dereferencing are not (because you don’t know the size of the underlying data). So yes, you can compare void pointers quite well (a).

    However, keep in mind that you don’t usually compare the pointers passed to a qsort comparison function, unless you want to sort on their addresses. But, since they’ll be sorted in that order already (being an array), there’s not much of a use case for that 🙂

    You generally cast the void pointers to a specific pointer and then compare what they point to. Something like:

    int compfn (const void *p1, const void *p2) {
        const char *str1 = *((const char **)(p1));
        const char *str2 = *((const char **)(p2));
        return strcmp (str1, str2);
    }
    

    You don’t have to create temporaries like str1 and str2 (even though any decent compiler would optimise them out anyway). Other than a slight readability issue, there’s nothing wrong with:

    int compfn (const void *p1, const void *p2) {
        return strcmp (*((const char **)(p1)), *((const char **)(p2)));
    }
    

    (a) Subject to the normal rules that the pointers must both point to elements of the same array or one beyond that array – anything else is undefined. I mention that for completeness but, if you’re using qsort, you’ll be working on an array anyway.

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

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I need to clean up various Word 'smart' characters in user input, including but
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to

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.