2

What is the difference between int *fun(void) and int fun*(void) ?

4
  • 5
    They look incomplete and out of context in different ways? Please clarify your question. Commented Mar 1, 2011 at 9:05
  • 2
    You mean except the fact that the second one is wrong and it won't compile ? Commented Mar 1, 2011 at 9:09
  • Why did you name this post pointer to a function? Commented Mar 1, 2011 at 10:42
  • 1
    Krtek's answer was sarcasm. The point is that's a huge difference and there's no point in trying to imagine some other difference. Commented Mar 1, 2011 at 12:21

3 Answers 3

6

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.

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

1 Comment

With in a minute difference, our answers turned out to be similar. I think, I should learn typing fast at least for the sake of S0 :)
3
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

0

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)

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.