-2

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;
}
Jinzu
  • 1,325
  • 2
  • 10
  • 22
  • It's being given `pf`, which is set to `sum`. – Barmar Feb 26 '19 at 00:52
  • Again?! [Didn't I just explain to you that a *function designator* is implicitly converted to a pointer to a function in almost all cases?](https://stackoverflow.com/questions/54820263/why-can-function-pointers-be-used-with-or-without-the-address-of-operator) – Antti Haapala -- Слава Україні Feb 26 '19 at 00:56
  • 1
    *"In the main function, `any_function` is never given any function pointers, only functions themselves."* False, the first call to `any_function` explicitly passes a function pointer. The other two calls implicitly pass function pointers. *"Does this mean the function pointer `any_function` is recursively calling itself?"* No, definitely not. – user3386109 Feb 26 '19 at 01:12
  • The question might be clearer if you remove two of the calls to `any_function`. See [mcve]. – user3386109 Feb 26 '19 at 01:13
  • @AnttiHaapala: Whoa, yes this is in fact the same problem I keep forgetting. Okay I understand now. – Jinzu Feb 26 '19 at 16:37

1 Answers1

1

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 = &sum;

Does this mean the function pointer any_function is recursively calling itself?

No, where did that idea come from?

user253751
  • 57,427
  • 7
  • 48
  • 90