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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:36:49+00:00 2026-06-17T03:36:49+00:00

Possible Duplicate: How do function pointers in C work? Surfing on stackoverflow I found

  • 0

Possible Duplicate:
How do function pointers in C work?

Surfing on stackoverflow I found this example:

/* Validation functions start */
void populate_array(int *array, size_t arraySize, int (*getNextValue)(void))
{
    for (size_t i=0; i<arraySize; i++)
        array[i] = getNextValue();
}

int getNextRandomValue(void)
{
    return rand();
}

int main(void)
{
    int myarray[10];
    populate_array(myarray, 10, getNextRandomValue);
    ...
}

I was wondering, imagine getNextRandomValue had a parameter getNextRandomValue(int i), how would I include this and making the function accepting inputs?

Many thanks

  • 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-17T03:36:51+00:00Added an answer on June 17, 2026 at 3:36 am

    Common practice is to pass a pointer to “data” together with the function. When function gets called, pass that “data” pointer into function and assume that the function itself knows what to do with that data. In fact the data is usually a pointer to a structure. So the code looks like this:

    struct func1_data {
        int a;
        int b;
    };
    
    struct func2_data {
        char x[10];
    };
    
    int function1(void *data) {
        struct func1_data *my_data = (typeof(my_data)) data;
        /* do something with my_data->a and my_data->b */
        return result;
    }
    
    int function2(void *data) {
        struct func2_data *my_data = (typeof(my_data)) data;
        /* do something with my_data->x */
        return result;
    }
    

    and assume we have

    int caller(int (*callback), void *data) {
        return callback(data);
    }
    

    Then you call all this like this:

    struct func1_data data1 = { 5, 7 };
    struct func2_data data2 = { "hello!" };
    caller(function1, (void *) &data1);
    caller(function2, (void *) &data2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What is the point of function pointers? I saw this code void
Possible Duplicate: Dereferencing void pointers I have a function call this way: void foo(void
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: calling ASP function from javascript okay running this code : <script type=text/javascript>
Possible Duplicate: How do I get this CSS text-decoration issue to work? I'm using
Possible Duplicate: are there function pointers in c#? I'm interested in finding the difference
Possible Duplicate: What is the point of function pointers? hi all, I want to
Possible Duplicate: Calling virtual functions inside constructors Look at this code. In the constructor
Possible Duplicate: what's the meaning of this piece of code? void (*signal(int sig, void
Possible Duplicate: Passing char pointer from C# to c++ function I have this type

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.