Anthony
Anthony

Reputation: 633

In C, does * also mean the same thing as void when used as a function argument?

For example, look at the following (abstract) declaration :

int (*) (*)

// pointer to a function returning an int and taking no arguments

(which I got from the following site : http://www.vineetgupta.com/blog/deciphering-complex-c-declarations)

I thought that only void means no arguments. Does this really mean the same thing as :

int (*) (void)

If so, where does it state that * can be used to specify no arguments?

Also, am I correct in assuming that abstract declarations such as this only exist for type casting? If so then it must be invalid in its current form since it lacks the surrounding parenthesis right? So int(*)(void) is invalid, but (int(*)(void)) is valid, no?

Upvotes: 4

Views: 179

Answers (2)

user529758
user529758

Reputation:

pointer to a function returning an int and taking no arguments

That's not right - the function takes a pointer as its argument, but since the type specifier is missing, the base type of the pointer is assumed to be int. This is ancient (pre-standard) behavior, some compilers allow it, others don't.

I thought that only void means no arguments. Does this really mean the same thing as :

int (*) (void)

No, it doesn't, for the reason explained above.

Also, am I correct in assuming that abstract declarations such as this only exist for type casting?

No, they can be used in function argument lists in a function declaration too. So,

int factorial(int);

is perfectly fine, so is

void qsort(void *, size_t, size_t, int (*)(const void *, const void *));

Upvotes: 13

Santhosh Pai
Santhosh Pai

Reputation: 2625

EDIT

 int (*) (*)  

results in an error when i tried in visual studio and its not the way of declaring.

If you want a function pointer to return an integer data with pointer as argument the correct way of doing it is below.

 int(*foo)(datatype *).

where data type can be

  int ,float,char etc

and void and * are not the same .

Upvotes: 0

Related Questions