0

How (*ptr_fun1)(10) and ptr_fun1(10) are same in the below code

Code:

#include<stdio.h>
void fun1(int a)
{
     printf("It is %d\n",a);
}
int main()
{
  void (*ptr_fun1)(int);
  ptr_fun1 = fun1;
/*
   ex-
        ptr_fun1    fun1    
        +----+      +-----+
        +1000+      +code +
        +----+      +-----+
         1234        1000
        fun1(10); <=> ptr_fun1(10); //how
*/
  printf("%d\n%d",ptr_fun1(10),(*ptr_fun1)(20));
  return 0;
}

output

10
20

Can some one please explain how it works.

4
  • When not used in a function call or as an operand for the address-of operator &, a function name is a pointer to the function. This means that if f is a function, f and &f are the same thing. Commented Oct 12, 2019 at 5:57
  • The function that you include (fun1) is not a function. It does not return anything. Try changing from void to return a value. Commented Oct 12, 2019 at 6:27
  • @dash-o: The C standard calls these functions regardless of whether they return a value. Commented Oct 12, 2019 at 8:55
  • The code, as included in the question, fail to compile because of the type issue: t3.c: In function ‘main’: t3.c:19:19: error: invalid use of void expression printf("%d\n%d",ptr_fun1(10),(*ptr_fun1)(20)); Commented Oct 12, 2019 at 15:11

1 Answer 1

1

The syntax for declaring (and using) function pointers in C (and C++) is one of the most puzzling aspects of the language for beginners. I shall try to explain a little, here.

First, let's consider pointers to 'simple' types (we'll take the 'int' type as an example). In this case, declaring a variable of the type is trivial: int N; Also, declaring a pointer to an int is also trivial: int *pN;We can interpret this second declaration in terms of 'evaluating' the '*' operator, which means "get the object that resides at the given address." So, in int *pN we are declaring that "the object that lies at the address "pN" is an int.

For functions, this is not so simple! Take a case of a function that takes an int as its (only) argument and returns an int value: int IFunc(int arg);. This is also very straightforward.

But how could we declare a pointer to such a function? We cannot simply apply the same logic as for the 'simple' types (by preceding with a * operator), like this:

int *pIFunc(int arg);

Because this would declare a function that takes an int arg and returns a pointer to an int.

So, the early implementors of the C language had to come up with something better - and completely unambiguous. So they decided to use the syntax of putting the "*NAME" section in parentheses, to isolate that 'dereference' operation from the function's definition:

int (*pIFunc)(int arg);

So, when faced with anything that looks remotely like this: < typename > (*Name1)(...); (where < typename > is any allowable C-type, like int, void, double, or even 'compound' types such as int*, and the "..." inside the second set of brackets can be either empty or a list of other 'types'), recognize it as a declaration of a function pointer (or as dereferencing a function pointer). To get the underlying function 'signature' (or invocation), just remove the first set of brackets and the contained *. So, for:

(*ptr_fun1)(20)

you can read:

ptr_fun1(20)

And, for:

void (*ptr_fun1)(int);

you can see that ptr_fun has the following signature:

void ptr_fun1(int);

I hope this makes things a bit clearer. Feel free to ask for further clarification and/or explanations.

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

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.