How can I create a "function pointer" (and (for example) the function has parameters) in C?
http://www.newty.de/fpt/index.html
typedef int (*MathFunc)(int, int);
int Add (int a, int b) {
printf ("Add %d %d\n", a, b);
return a + b; }
int Subtract (int a, int b) {
printf ("Subtract %d %d\n", a, b);
return a - b; }
int Perform (int a, int b, MathFunc f) {
return f (a, b); }
int main() {
printf ("(10 + 2) - 6 = %d\n",
Perform (Perform(10, 2, Add), 6, Subtract));
return 0; }
typedef int (*funcptr)(int a, float b);
funcptr x = some_func;
int a = 3;
float b = 4.3;
x(a, b);
I found this site helpful when I was first diving into function pointers.
http://www.newty.de/fpt/index.html
First declare a function pointer:
typedef int (*Pfunct)(int x, int y);
Almost the same as a function prototype.
But now all you've created is a type of function pointer (with typedef).
So now you create a function pointer of that type:
Pfunct myFunction;
Pfunct myFunction2;
Now assign function addresses to those, and you can use them like they're functions:
int add(int a, int b){
return a + b;
}
int subtract(int a, int b){
return a - b;
}
. . .
myFunction = add;
myFunction2 = subtract;
. . .
int a = 4;
int b = 6;
printf("%d\n", myFunction(a, myFunction2(b, a)));
Function pointers are great fun.
You can also define functions that return pointers to functions:
int (*f(int x))(double y);
f is a function that takes a single int parameter and returns a pointer to a function that takes a double parameter and returns int.
Related
I'm trying to make a program that receives a pointer to a function and the arguments, and then calls the function with the arguments. The best way to do it seems to be passing the arguments as an array, but I can't change the arguments accepted by the function.
This is similar to what I would like to do.
int add(int a, int b){
return a + b;
}
int add3(int a, int b, int c){
return a + b + c;
}
int main(){
int* args = calloc(sizeof(int) * 2);
add(args);
args = calloc(sizeof(int) * 3);
add3(args);
}
I have a quastion about C functions. Is there any possibility to do something like:
#define PRINT_SUM_OF_CONSTS() printSum(10, 5)
void printSum(int a, int b){
print("%d + %d = %d", a, b, a+b);
}
int main(){
void (*pFunc)(void);
pFunc = &PRINT_SUM_OF_CONSTS;
pFunc();
return 0;
}
What I need is to use function which takes two arguments, asign arguments to constants with a macro and use it as function without arguments. Is that somehow possible?
pFunc is a pointer to a function, you cannot create a pointer to a function call with specific parameters.
However, the solution is simple and does not require a macro - you simply create a wrapper function that calls the target function with the desired parameters:
#include <stdio.h>
void printSum(int a, int b)
{
printf("%d + %d = %d", a, b, a+b);
}
void printSumConst() { printSum(10, 5) ; }
int main()
{
void (*pFunc)(void) = printSumConst;
pFunc();
return 0;
}
void f(int a, char b, char* c) {
if(..) {
...
f(a,b,c); //recursive
}
...
}
void g(int a, double b, char* c, int d) {
if(..) {
...
g(a,b,c,d); //recursive
}
...
}
I want to make a separate function because I use the code within the if statement several times. But this function have to have a function as a parameter becuase I use recursive method. I know that we can use function as a parameters, but in the f function there are 3 parameters, in the g function have 4 parameters.
The code in the if statement in f is the same as the code in the if of g. Except for the function call in that code?
Simply I have no idea how to solve this issue.
You can use union to pack the variable number of arguments, as shown in sample code below.
It may be unusual to use union like this, but it works.
#include<stdio.h>
union u {
struct { int a; char b; char* c; } f;
struct { int a; double b; char* c; int d; } g;
};
void func_u_f (union u* ua) {
printf(" f = {a: %d, b: %c, c:%s}\n", ua->f.a, ua->f.b, ua->f.c);
ua->f.a++;
}
void func_u_g (union u* ua) {
printf(" g = {a: %d, b: %e, c:%s, d:%d}\n",
ua->g.a, ua->g.b, ua->g.c, ua->g.d);
ua->g.a++; ua->g.b *= 2.0; ua->g.d++;
}
void r (int i, void (*func) (union u*), union u* ua) {
if (i < 3) { /* or whatever conditions to terminate recursion */
printf ("Recursion %d\n", i);
func(ua);
r (++i, func, ua);
} else {
printf ("Exit recursion at %d\n", i);
return;
}
}
int main () {
union u u1, u2;
/* f */
u1.f.a = 10; u1.f.b = 'X'; u1.f.c = "I am u.f.";
r(0, &func_u_f, &u1);
/* g */
u2.g.a = 10; u2.g.b = .4e-6; u2.g.c = "I am u.g."; u2.g.d = 98;
r(-2, &func_u_g, &u2);
return 0;
}
I am proposing you an easy fix that doesn't work in general because it involves the use of a sentinel value.
Let's say that the the variable d that you pass to g is always positive. Than you can choose -1 as sentinel value.
You always pass four parameters to the function myIf and then check whether d is -1. If it is, then you call f with three parameters, otherwise you call g.
int main() {
myIf(a, b, c, d);
return 0;
}
void myIf(int a, int b, char *c, int d)
{
if( d == -1 ) {
f(a, b, c);
}
else {
g(a, b, c, d);
}
}
I'm not sure exactly what you're trying to do. But if you're worried about code duplication in the if statements, you can refactor that our to another function:
if (isCondition()) ...
bool isCondition() { return ... }
How does the function pointer in the declaration of any_function receive any value? In the main function, any_function is never given any function pointers, only functions themselves. Does this mean the function pointer any_function is recursively calling itself?
#include <stdio.h>
int sum(int, int);
int product(int, int);
int difference(int, int);
int any_function(int(*pfun)(int, int), int x, int y);
int main(void)
{
int a = 10; /* Initial value for a */
int b = 5; /* Initial value for b */
int result = 0; /* Storage for results */
int (*pf)(int, int) = sum; /* Pointer to function */
/* Passing a pointer to a function */
result = any_function(pf, a, b);
printf("\nresult = %d", result );
/* Passing the address of a function */
result = any_function(product,a, b);
printf("\nresult = %d", result );
printf("\nresult = %d\n", any_function(difference, a, b));
return 0;
}
/* Definition of a function to call a function */
int any_function(int(*pfun)(int, int), int x, int y)
{
return pfun(x, y);
}
/* Definition of the function sum */
int sum(int x, int y)
{
return x + y;
}
/* Definition of the function product */
int product(int x, int y)
{
return x * y;
}
/*Defintion of the function product*/
int difference(int x, int y)
{
return x - y;
}
How does the function pointer in the declaration of any_function receive any value?
Because the function call specified a value. Same as any other parameter.
E.g. when you write:
int my_function(int x) {
return x + 1;
}
// in main
printf("%d\n", my_function(5));
it prints 6. But how did x in the declaration of my_function receive any value? Well, it was specified when we wrote my_function(5).
In the main function, any_function is never given any function pointers, only functions themselves.
If you use a function like that it is converted to a pointer automatically. In other words pf = sum; is short for pf = ∑
Does this mean the function pointer any_function is recursively calling itself?
No, where did that idea come from?
#include <stdio.h>
int Add(int a, int b);
int Add(int a, int b, int c);
double Add(double a, double b);
void main()
{
printf("1+2=%d\n",Add(1,2));
printf("3+4+5=%d\n",Add(3,4,5));
printf("1.414+2.54=%f\n",Add(1.414,2.54));
}
int Add(int a, int b)
{
return a+b;
}
int Add(int a, int b, int c)
{
return a+b+c;
}
double Add(double a, double b)
{
return a+b;
}
I wrote with C language and using Xcode. While studying "overload", Xcode keeps show error message that overload cannot be worked. Instead it shows "Conflicting types for 'Add'" message.
With Xcode, would overload cannot be worked?
In Simple words, C doesn't allow function overloading! So, you can't write multiple functions with the same name!
Try to give different function name and try-
#include <stdio.h>
int Add2int(int a, int b);
int Add3int(int a, int b, int c);
double Add(double a, double b);
void main()
{
printf("1+2=%d\n",Add2int(1,2));
printf("3+4+5=%d\n",Add3int(3,4,5));
printf("1.414+2.54=%f\n",Add(1.414,2.54));
}
int Add2int(int a, int b)
{
return a+b;
}
int Add3int(int a, int b, int c)
{
return a+b+c;
}
double Add(double a, double b)
{
return a+b;
}
As mentioned C doesn't support function overloading (like in C++). Neverthless C99 introduced function-like macros with empty arguments, however commas must be preserved with exact number. Here is an example:
#include <stdio.h>
#define Add(a,b,c) (a+0)+(b+0)+(c+0)
int main(void)
{
int a = 1, b = 2, c = 3;
double x = 1.5, y = 2.25, z = 3.15;
printf("%d\n", Add(a, b, c)); /* 6 */
printf("%d\n", Add(a, b, )); /* 3 */
printf("%d\n", Add(, , c)); /* 3 */
printf("%g\n", Add(, y, z)); /* 5.4 */
printf("%g\n", Add(x, , )); /* 1.5 */
return 0;
}
Note that due to due usual arithmetic conversions for arguments with floating-point type 0 would be properly promoted to such type.
This may be only possible if you are using C++.
But in C you can't think of function overloading.
So try to change the function name and then it will work.
Actually C language does not allow function or method overloading
To do method overloading you have to go to C++ or Java