0

In a book, I came across the following question. The problem which I am facing is, what is a function pointer? How does it work and what is the syntax for it's declaration.

Point the error in given code.

main()
{
    int (*p)()=fun;
    (*p)();
}
fun()
{
    printf("Hi..");
}

my first question is, What does following code snipet signifies?

int (*p)()=fun;
(*p)();

and second obvious question what is the error in the given code..??

refer to ionela.voinescu's answer for solution.. it is same as that written in solution manual..thnx

DD24
  • 61
  • 7

3 Answers3

5

int (*p)() = fun declares a pointer to a function that returns int, then assigns the address of the function fun to that pointer. (*p)() calls whatever function p is pointing to.

Problems with your code:

  1. fun and main should have a return type. Your compiler might not demand it and assume they return ints, but you should give them one nonetheless.

  2. You need to declare fun above main, or use a prototype

Also, (*p)() is unnecessary; you can just use p().

As a side note, because function pointer syntax is relatively ugly, it's fairly common to see typedefs such as

typedef int(*IntFunc)();

which would allow you to declare and use p like so:

IntFunc p = fun;
p();
Matt Kline
  • 10,149
  • 7
  • 50
  • 87
4

The correct code is the following:

#include <stdio.h>

int fun();
int main(){
  int (*p)()=fun;
  (*p)();
  return 0;
}

int fun(){
  printf("Hi..");
  return 0; 
}

1.
int fun(); You have to declare your function before using it in main; Otherwise main wont recognize it.
2.
int (*p)() : Declaration of a variable p which is a pointer to a function that returns int and has no arguments(or undefined number of arguments, depending on the standard).
3.

int fun();
.........
int (*p)()=fun;

In order for this to work fun must also return int. Meaning when assigning a value to a variable this must have a type which corresponds with the declaration of the variable. When assigning a value to a pointer to a function that returns int and has no arguments the function assigned to it must also return int and have no arguments (or undefined number of arguments, depending on the standard).
4.
(*p)();
You call the function p which now points to fun.

ionela.voinescu
  • 384
  • 1
  • 7
  • +1 for including the header – doctorlove Jul 16 '13 at 14:17
  • Ah - so you do want to declare the functions before you use them. What book is this? – doctorlove Jul 16 '13 at 14:25
  • Test Your C Skills by Yeshwant Kanetkar.. – DD24 Jul 16 '13 at 14:29
  • Depending of your compiler flags, it may generate an implicit declaration of ``fun()`` and will report a warning. So you should declare it, but you don't have to. – linkdd Jul 16 '13 at 14:33
  • ionela.voinescu - in place of int .. cant v use float or any other data type for function fun while declaring and defining it when the value is being returned..?? – DD24 Jul 16 '13 at 15:14
  • @DD24 - I don't know if I understood your question. Do you mean if you can declare and define `fun` to return `float` or other data type, but still use it the same way in the initialization of pointer `p`? The answer is yes, in this case. The compiler will warn you with `initialization from incompatible pointer type` when assigning the address of `fun` to `p`, but it will compile without errors. The problem appears if you use the value returned by `fun`. Even if you return a `float` value, in `main` after the execution of `p`, the return value will be interpreted as `int`. – ionela.voinescu Jul 16 '13 at 15:28
1

fun is name of the the function itself.

int (*p)()

is the definition of the function pointer like 'int a' is the definition of an integer.

int * is the pointer itself, () means it is a pointer to a function.

(*p)();

This is the execution of the function of the function pointer p, i.e. p is assigned to the function fun, which is executed by calling the content of the pointer p, thus (*p)();

Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
  • -thnx for your help.. plz assure me if got the ryt concept.. int(*p)(); is the declaration of function pointer.. by writing int(*p)()=fun ..we are assigning the address of first statment of function defination to pointer p.. and in the next statment (*p)() we are executing the function using the address of first statmnet of the function defination stored by the function pointer p... plz tell me .. if i am correct.. and this is what you wanted to explain me...??? – DD24 Jul 16 '13 at 14:13
  • @DD24 I dont think that function pointer would point first statement of function. This link may help you for certain extent http://stackoverflow.com/questions/597292/where-exactly-do-function-pointers-point . If you cant find any concrete solution, you can post it as separate question. – VoidPointer Jul 16 '13 at 14:25
  • Michel -thnx bro .. the link was helpful...thnx a lot – DD24 Jul 16 '13 at 14:33
  • @DD24: yes you got it right. However the first statement pointer is more or less the same as the 'entry point' of the function. – Michel Keijzers Jul 16 '13 at 15:19