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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:54:53+00:00 2026-05-14T01:54:53+00:00

I have a function which takes a block of data and the size of

  • 0

I have a function which takes a block of data and the size of the block and a function pointer as argument. Then it iterates over the data and performes a calculation on each element of the data block.
The following is the essential outline of what I am doing:

int myfunction(int* data, int size, int (*functionAsPointer)(int)){
    //walking through the data and calculating something
    for (int n = 0; n < size; n++){
        data[n] = (*function)(data[n]);
    }
}

The functions I am passing as arguments look something like this:

int mycalculation(int input){
    //doing some math with input
    //...
    return input;
} 

This is working well, but now I need to pass an additional variable to my functionpointer. Something along the lines

int mynewcalculation(int input, int someVariable){
    //e.g.
    input = input * someVariable;
    //...
    return input;
}

Is there an elegant way to achieve this and at the same time keeping my overall design idea?

  • 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-14T01:54:53+00:00Added an answer on May 14, 2026 at 1:54 am

    The usual way to make it totally generic is with a void*, which of course causes all kinds of typesafety issues:

    int map_function(int* data, int size, int (*f_ptr)(int, void*), void *userdata){
        //walking through the data and calculating something
        for (int n = 0; n < size; n++){
            data[n] = (*f_ptr)(data[n], userdata);
        }
    }
    
    int simple_calculation(int input, void *userdata) {
        // ignore userdata and do some math with input
        return input;
    }
    
    int calculation_with_single_extra_arg(int input, void *userdata) {
        int input2 = * (int*) userdata;
        // do some math with input and input2
        return input;
    }
    
    int calculation_with_many_extra_args(int input, void *userdata) {
        my_data_t data = (my_data_t *) userdata;
        return input * (input * data->a + data->b) + data->c;
    }
    
    int main(int argc, char **argv) {
        int array[100];
        my_data_t userdata = { 1, 2, 3 };
    
        populate(array, 100);
    
        map_function(data, calculation_with_many_extra_args, &userdata);
    }
    

    Any given function to be passed in must have that prototype, but you can shove any data you want in through userdata. There’s just absolutely no typechecking.

    You could also use va_args as dbingham suggests; this isn’t really much different though, since the prototype for your function to map will have to be int(int, va_list).

    Edit: I favor the void* approach. The va_list doesn’t add any typesafety anyway and adds more potential for user error (particularly calling va_end twice or not implementing the va_arg loop right). The void* also doesn’t add any extra lines of code; it’s passed cleanly through, and the user just dereferences it into (hopefully) the right type (which is probably a struct, in the general case).

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

Sidebar

Related Questions

Say I have a C function which takes a variable number of arguments: How
So: I have the following function, adapted from a formula found online, which takes
I have a function which searches an STL container then returns the iterator when
I have an function which decodes the encoded base64 data in binary data but
i have a function which retrieves values from a webservice , then loops through
I have a function which parses one string into two strings. In C# I
I have a function which gets a key from the user and generates a
Imagine I have an function which goes through one million/billion strings and checks smth
This is a Windows Forms application. I have a function which captures some mouse
i have a c function which returns a long double . i'd like 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.