svKris
svKris

Reputation: 783

how to call a function whose name is the same of the local variable name in c

How can i call a function whose name is same as that of the local variable in a calling function

Scenario:

I need to call a function myfun(a,b) from some other function otherfun(int a,int myfun) . How can i do it?

int myfun(int a , int b)
{
 //
//
return 0;
}


int otherfun(int a, int myfun)
{
 // Here i need to call the function myfun as .. myfun(a,myfun)
 // how can i do this?? Please help me out

}

Upvotes: 1

Views: 176

Answers (3)

Jens Gustedt
Jens Gustedt

Reputation: 78923

The best idea would be to just chose a different name for your parameter. The second best is this one, I think:

int otherfun(int a, int myfun)
{
 int myfun_tmp = myfun;
 // Here i need to call the function myfun as .. myfun(a,myfun)
 {
   extern int myfun(int, int);
   myfun(a, myfun_tmp);
 }
}

Upvotes: 0

Michał Górny
Michał Górny

Reputation: 19253

You can create a variable keeping a pointer to the myfun() function. This will allow you to effectively 'alias' the original function without introducing an additional one.

int myfun(int a, int b)
{
    // ...
    return 0;
}

static int (*myfunwrap)(int, int) = &myfun;

int otherfun(int a, int myfun)
{
    myfunwrap(a, myfun);
}

Of course, you can replace myfunwrap with any name you like.

Upvotes: 0

Nordic Mainframe
Nordic Mainframe

Reputation: 28747

int myfun(int a , int b)
{
return 0;
}

int myfun_helper(int a, int b) 
{
 return myfun(a,b);
}
int otherfun(int a, int myfun)
{
 /* the optimizer will most likely inline this! */
 return myfun_helper(a,myfun);
}

Upvotes: 8

Related Questions