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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:15:42+00:00 2026-05-31T01:15:42+00:00

I would like to have an array of function pointers, each pointing to a

  • 0

I would like to have an array of function pointers, each pointing to a differ function. The function could differ also in the prototype, and number of parameters.

I am looking for the following similar functionality in C/C++.

The following code is not compilable in C

#include <stdio.h>

typedef int (*FUNC)(int a,int b);

int func_one(int a)
{
   printf("\n In function 1 with 1 parameter %d \n",a);
   return 1;
}

int func_two(int a,int b)
{
   printf("\n In function 2 with 2 parameter %d %d \n",a,b);
   return 2;
}

typedef struct{
FUNC fnc;
enum type{ ONE,TWO} type_info;
}STR;

int main()
{
STR str[2];
int ret;
int i;

str[0].fnc = func_one;
str[0].type_info = ONE;

str[1].fnc = func_two;
str[1].type_info = TWO;


for(i=1;i>=0;--i)
{
   if(str[i].type_info == ONE)
      ret = str[i].fnc(10);
   else if(str[i].type_info == TWO)
      ret = (str[i].fnc)(10,20);
   else
      perror("error in implementation \n");

       printf("\n return value is %d \n",ret);
     }
return 0;
}
  • 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-31T01:15:44+00:00Added an answer on May 31, 2026 at 1:15 am

    In C, it is safe to cast from one function-pointer type to another (as long as you cast it back in order to call it), so you can declare a sort of “generic function-pointer type”:

    typedef void (*GENFUNC)(void);
    

    and then cast as needed:

    GENFUNC tmp = (GENFUNC)&func_two; // cast to generic pointer
    
    FUNC two = (FUNC)tmp; // note: have to cast it back!
    two(0, 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have an array (or a vector) and at each position
I would like to have a function that returns something like an array of
I have the array shown below. I would like to have a resultant array
I have an array that I would like to sort using a date field
I have an array of 50 items and I would like to choose 5
i have an array $mainArray of arrays and i would like to remove /
I have an array of sprites called segments and I would like to skip
We have an array of points through which we would like to draw a
I have an array of integers which represent a RGB image and would like
I have an array of dictionaries. I would like to extract an array with

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.