2

I want to be able to call multiple functions using a single generic function call.

Currently I am trying to implement something like this:

main()
{
    generic();
}

A(){.....};

B(){.....};

C(){.....};

Where I call from main() some generic function, which, in turn, is supposed to be able to call the functions: A(), B(), C().

How can I implement this in C?

8
  • you can simply declare the functions in constructor Commented Mar 19, 2014 at 6:58
  • 1
    Please clarify your question. Do you want generic to always call all of the funtions - or do you want it to call one of the functions based on a parameter? Commented Mar 19, 2014 at 6:58
  • 1
    @HeenaGoyal Do we have constructor in C ? Commented Mar 19, 2014 at 7:42
  • @HeenaGoyal: There is no constructor in c. But do you have any other way to do it Commented Mar 19, 2014 at 8:11
  • Yes i want generic to call all functions without specifying there names , something like packet broadcast in computer networks Commented Mar 19, 2014 at 8:12

3 Answers 3

2

You can do it by using function pointers. Please refer on "how to use function pointers in C"

Sign up to request clarification or add additional context in comments.

2 Comments

i refered that but i could't find the answer.Give me any reference link if you have thanks
please see answer posted by user207064 below. Hope it helps you
1

You have to use function pointers as below.

#include<stdio.h>
void A(void);
void B(void);
void C(void);
typedef void (*foo_ptr_t)( void );
foo_ptr_t foo_ptr_array[3]; //fucntion pointers of type foo_ptr_t
main()
{
    int i = 0;
    foo_ptr_array[0] = A; //assign function to each function pointer
    foo_ptr_array[1] = B;
    foo_ptr_array[2] = C;
    for(i = 0; i<3; i++)
        foo_ptr_array[i](); //call functions using function pointer
}

void A()
{
    printf("We are in A\n");
}

void B()
{
    printf("We are in B\n");
}

void C()
{
    printf("We are in C\n");
}

4 Comments

thanks for answer but i wanted this more dynamic suppose if we dont know function names in project, how to achieve that?.
if you don't know function names, then how do you want to call them? What DO you know about them then?
ok then suppose if we have function names and other details. i want to check one global variable in main function by all functions in project. But in C always program start from main() i always had to call that function in order to make that function work. how I can make these function automatically check value of global
extern int a=1; main() { a=1; generic();// this is generic call which invokes all fun. } int A(parameters) { if (a==1){ //do something } char B(parameters) { if (a==1){ //do something }
0

You can use function pointers, if all functions you want to call have same prototype.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.