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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:41:31+00:00 2026-06-14T14:41:31+00:00

I have an incr function to increment the value by 1 I want to

  • 0

I have an incr function to increment the value by 1
I want to make it generic,because I don’t want to make different functions for the same functionality.

Suppose I want to increment int,float,char by 1

void incr(void *vp)
{
        (*vp)++;
}

But the problem I know is Dereferencing a void pointer is undefined behaviour. Sometimes It may give error :Invalid use of void expression.

My main funciton is :

int main()
{

int i=5;
float f=5.6f;
char c='a';

incr(&i);
incr(&f);
incr(&c);

return 0;
}

The problem is how to solve this ? Is there a way to solve it in Conly

or

will I have to define incr() for each datatypes ? if yes, then what’s the use of void *

Same problem with the swap() and sort() .I want to swap and sort all kinds of data types with same function.

  • 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-14T14:41:32+00:00Added an answer on June 14, 2026 at 2:41 pm

    You can implement the first as a macro:

    #define incr(x) (++(x))
    

    Of course, this can have unpleasant side effects if you’re not careful. It’s about the only method C provides for applying the same operation to any of a variety of types though. In particular, since the macro is implemented using text substitution, by the time the compiler sees it, you just have the literal code ++whatever;, and it can apply ++ properly for the type of item you’ve provided. With a pointer to void, you don’t know much (if anything) about the actual type, so you can’t do much direct manipulation on that data).

    void * is normally used when the function in question doesn’t really need to know the exact type of the data involved. In some cases (e.g., qsort) it uses a callback function to avoid having to know any details of the data.

    Since it does both sort and swap, let’s look at qsort in a little more detail. Its signature is:

    void qsort(void *base, size_t nmemb, size_t size,
               int(*cmp)(void const *, void const *));
    

    So, the first is the void * you asked about — a pointer to the data to be sorted. The second tells qsort the number of elements in the array. The third, the size of each element in the array. The last is a pointer to a function that can compare individual items, so qsort doesn’t need to know how to do that. For example, somewhere inside qsort will be some code something like:

    // if (base[j] < base[i]) ...
    if (cmp((char *)base+i, (char *)base+j) == -1)
    

    Likewise, to swap two items, it’ll normally have a local array for temporary storage. It’ll then copy bytes from array[i] to its temp, then from array[j] to array[i] and finally from temp to array[j]:

    char temp[size];
    
    memcpy(temp, (char *)base+i, size);              // temp = base[i]
    memcpy((char *)base+i, (char *)base+j, size);    // base[i] = base[j]
    memcpy((char *)base+j, temp, size);              // base[j] = temp
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes (CoinFlip and Incr) in different files and I'm trying to
Have deployed numerous report parts which reference the same view however one of them
I have a jquery mouseover event which succesfully calls a javascript function $(document).ready(function() {
I have a range of input buttons, each with its own value. <ul class=sizeDriller>
I would like to have the user call my function and then have the
I have 2 textbox and 1 button textbox1,textbox2 and 1 increment button.Both textbox is
Suppose I have a CMS application written in Node.js which persists data on a
I have asked a question about Incr Pool Size or Decr Pool Size How
I want to find a pattern in a file, but the pattern can have
I have a single-threaded linux app which I would like to make parallel. It

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.