Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to understand "typedef int (xxx)(int yyy);"?

Tags:

c

syntax

typedef int (xxx)(int yyy); seems to define a function pointer named xxx which points to a function with a integer parameter yyy.

But I can't understand that syntax...Could any one give a good explanation?


I find typedef int xxx(int yyy); still works. Any difference between them?

like image 291
Sayakiss Avatar asked Aug 27 '13 03:08

Sayakiss


1 Answers

This defines a function type, not a function pointer type.

The pattern with typedef is that it modifies any declaration such that instead of declaring an object, it declares an alias to the type the object would have.

This is perfectly valid:

typedef int (xxx)(int yyy); // Note, yyy is just an unused identifier.
 // The parens around xxx are also optional and unused.

xxx func; // Declare a function

int func( int arg ) { // Define the function
    return arg;
}

The C and C++ languages specifically, and mercifully, disallow use of a typedef name as the entire type in a function definition.

like image 94
Potatoswatter Avatar answered Oct 12 '22 21:10

Potatoswatter