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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:01:30+00:00 2026-06-14T17:01:30+00:00

In stdlib.h , there is a declaration of the function qsort() with prototype: void

  • 0

In stdlib.h, there is a declaration of the function qsort() with prototype:

void qsort( void *ptr, size_t count, size_t size,
        int (*comp)(const void *, const void *) );

Obviously it is a generic programming.
I wonder how it implement, how to get the element from the void * type?

  • 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-14T17:01:31+00:00Added an answer on June 14, 2026 at 5:01 pm

    void * pointers are casted according to the size_t size (the 3rd argument in qsort)

    First we typecast the void* to char* and then do pointer arithmetic according to the size (because char takes 1 byte so adding the size will give correct pointer arithmetic )

    EDIT : (for in built data types)

    char *ptr = (char*)vp; //here vp is void *p
    
    *(ptr + (n-1)*size); //will give you nth element 
    

    e.g.

    size =1 and want 3rd element it means it will give you 3rd char
    size =4 and want 3rd element it means it will give you 3rd int or float
    size =2 and want 3rd element it means it will give you 3rd short int
    size =8 and want 3rd element it means it will give you 3rd double
    

    NOTE : size is implementation defined so it may vary to compilers

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

Sidebar

Related Questions

There is a function called div in C,C++ (stdlib.h) div_t div(int numer, int denom);
Here's a simple C file: #include <stdio.h> #include <stdlib.h> int main() { printf(hi there!\n);
Is there anyway I can modify this code example #include <stdlib.h> #include <iostream> class
So I have some class starting with #include <wchar.h> #include <stdlib.h> and there is
#include<stdlib.h> #include<string.h> #include<stdio.h> int pstrcmp( char **p,char **q) { return strcmp(*p,*q) ; } int
I've used #include<stdlib> #include<time> using namespace std; srand((unsigned)time(0)); int n=(rand()>>8)%4; but what other random
I created a program called test: #include<stdlib.h> #include<iostream> int main() { std::cout<<system(..\\add\\debug\\add.exe 4 8);
main.c (with all the headers like stdio, stdlib, etc): int main() { int input;
I have such a program: #include <stdlib.h> #include <iostream> static int pswd=0; int main()
Is there any standard implementation (meaning stdlib or boost) of an iterator that wraps

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.