[英]How to remove top-level pointer from typename?
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. 我想要轻松声明'pFuncType'指向的类型的函数。 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. 意味着intptr
是新类型。 A new variable of type intptr
can be defined as intptr
类型的新变量可以定义为
intptr p; // Same as int * p;
Using intptr
it is not possible to declare a variable of type int
. 使用intptr
无法声明int
类型的变量。 Doing 干
(*inptr) i;
is equivalent to 相当于
(*(int *)) i;
which is a wrong syntax. 这是一个错误的语法。
The same hold true with pFuncType
. pFuncType
。
Also note that the rule of declaration is that declation specifier must comes before the declarators . 另请注意,声明规则是declation说明符必须在声明 符之前。 *
comes in declarators, therefore it can't come before any declaration specifier . *
来自声明符 ,因此它不能出现在任何声明说明符之前。 Look at the grammar 1 看一下语法 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 1语法可以在C11标准的§6.7和§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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.