For example if I have:
typedef int (*pFuncType)(int a, char b);
And I want for ease to declare a function of the type which 'pFuncType' is pointer to. Something like this:
(*pFuncType) funcDeclr;
Is this even possible?
Is this even possible?
This is not possible. The declaration
typedef int * intptr;
means intptr
is of new type. A new variable of type intptr
can be defined as
intptr p; // Same as int * p;
Using intptr
it is not possible to declare a variable of type int
. Doing
(*inptr) i;
is equivalent to
(*(int *)) i;
which is a wrong syntax.
The same hold true with pFuncType
.
Also note that the rule of declaration is that declation specifier must comes before the declarators . *
comes in declarators, therefore it can't come before any declaration specifier . Look at the grammar 1
declaration
declaration-specifiers ;
declaration-specifiers init-declarator-list ;
declaration-specifiers
storage-class-specifier
type-specifier
type-qualifier
storage-class-specifier declaration-specifiers
type-specifier declaration-specifiers
type-qualifier declaration-specifiers
init-declarator-list
init-declarator
init-declarator-list , init-declarator
init-declarator
declarator
declarator = initializer
......
......
declarator
direct-declarator
pointer direct-declarator
direct-declarator
identifier
( declarator )
direct-declarator [ ]
direct-declarator [ constant-expression ]
direct-declarator ( )
direct-declarator ( parameter-type-list )
direct-declarator ( identifier-list )
pointer
*
* pointer
* type-qualifier-list
* type-qualifier-list pointer
1 The grammar can be found in C11 standard under the section §6.7 and §6.7.6
No. You can declare a non-pointer type, though:
typedef int (funcType)(int a, char b);
funcType f;
int f(int a, char b) {
return a;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.