What is the difference between int *fun(void) and int fun*(void) ?
-
5They look incomplete and out of context in different ways? Please clarify your question.Timbo– Timbo2011-03-01 09:05:16 +00:00Commented Mar 1, 2011 at 9:05
-
2You mean except the fact that the second one is wrong and it won't compile ?krtek– krtek2011-03-01 09:09:53 +00:00Commented Mar 1, 2011 at 9:09
-
Why did you name this post pointer to a function?Lundin– Lundin2011-03-01 10:42:32 +00:00Commented Mar 1, 2011 at 10:42
-
1Krtek's answer was sarcasm. The point is that's a huge difference and there's no point in trying to imagine some other difference.Jim Balter– Jim Balter2011-03-01 12:21:44 +00:00Commented Mar 1, 2011 at 12:21
3 Answers
The former starts to define (or declare, depending on what comes next) a function that returns a pointer to int. The latter is a syntax error. Neither are function pointers.
A function pointer needs to look like this:
int (*fun)(void);
This declares a pointer to a function that returns int.
1 Comment
int* fun(void);
fun do not take any arguments but returns a pointer to an integer. And I think, second one is syntactically wrong. If you are trying to for a function pointer, then it should be -
int (*fun)(void)= NULL;
Now, fun is pointer to a function that takes no arguments but returns an integer.
Comments
int* func(void)
Generally a valid declaration has 3 three parts, and end with ; semicolon.
Part 1) Argument
void mean - no argument (argument can be as complicated and vary in number)
Part 2) Return Type (can be user type or build in)
int* mean - (return type) is pointer to int
Part 3) Name of the Function (can be anything, should be logical to the operation it is going to perform.
func mean - name of the function
In the second syntex. we have * in between part 2 and part 3.
which is not allowed and meaningful. either you can add * to void or int.
int * func (void)
int func (void *)
or for function pointer as answered.
(*pf) ();
or
(*pf) (void)