4
int f1(){}
int* f2(){}

int main()
{
    int *a;//1
    int b;
    int *c;
    a=c; 
    a=&b; 

    int (*p)(); //2
    p=f2; //showing error
    p=&f1;
}

I expected that in my program '2' must behave similar to '1'. Why function pointer are behaving differently. Or am I missing something?

Alex
  • 1,178
  • 3
  • 9
  • 24
  • 5
    `f2` is a function returning a pointer to `int` (and would be converted into an `int * (*)()` in the line `p = f2;` if it compiled). `p` is a pointer to a function returning `int`. Incompatible types. – Daniel Fischer Jul 01 '13 at 12:44
  • Here's a tip to help declare function pointer types easily: `typedef int type_of_f1();`. Now just add a star and you have `type_of_f1* p;` to declare a function pointer suitable for f1. – R. Martinho Fernandes Jul 01 '13 at 12:54
  • A good [book](www.cs.rit.edu/~ats/books/ooc.pdf) – Grijesh Chauhan Jul 01 '13 at 13:15
  • possible duplicate of [Why do all these crazy function pointer definitions all work? What is really going on?](http://stackoverflow.com/questions/6893285/why-do-all-these-crazy-function-pointer-definitions-all-work-what-is-really-goi) – David Rodríguez - dribeas Jul 01 '13 at 13:23

5 Answers5

3
int* f2(){}

A function that accepts nothing, and returns a pointer to an int.

int (*p)();

A pointer to: A function that accepts nothing and returns an int

You have a type mismatch. p is not a pointer to the type of f2

If you have trouble understanding such definitions, like all mortal do, use the spiral rule of thumb

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
3

p=f2; error because incompatible types. f2 is a function that can return a int* whereas p is pointer to function that can point to a function returns int e.g. f1()

for int* f2(), you can defined a pointer to function as below:

 int* (*p2)();   // pointers to function f2
 p2 = f2;

Additionally, you don't need to use & before function name just function name is enough. Here is a good link to read: Why do all these crazy function pointer definitions all work? What is really going on?

Edit:
Some time &functionname and functionname are not same e.g. sizeof(functionname) is not valid whereas sizeof(&functionname) is perfectly valid.

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
2

This is a function that returns int*

int* f2(){}

So you need:

(int*) (*q)();
q = f2; 
johnchen902
  • 9,531
  • 1
  • 27
  • 69
1

p is pointer to function taking void argument and returning integer.

f1 is a function taking void argument and returning integer.

f2 is a function taking void argument and returning pointer to integer.

Now as per definitions you can see f1 can be assigned to p but f2 can't.To assign f2 in p, declaration of p should be int *(*p)();

Dayal rai
  • 6,548
  • 22
  • 29
1

You have made wrong assignments i.e incompatible types

For the functions

int f1(){}
int* f2(){}

The correct assignments would be

int (*p)(); 
p = f1;

int* (*p)();
p = f2;
GeekFactory
  • 399
  • 2
  • 13