简写typedef指向常量结构体的指针 [英] shorthand typedef pointer to a constant struct
问题描述
使用typedef声明结构
Declaring a struct with typedef
typedef struct some_struct {
int someValue;
} *pSomeStruct;
然后将它作为参数传递给一些带有const声明的函数,意味着'const some_struct * var'
and then passing it as a parameter to some function with const declaration, implying 'const some_struct * var'
void someFunction1( const pSomeStruct var )
原来是
some_struct * const var
这也在ISO C标准的第6.7.5.1节中说明,其中const在这种情况下适用于指针,而不是到它所指向的数据。
This is also stated in Section 6.7.5.1 of the ISO C standard which states that 'const' in this case applies to the pointer and not to the data to which it points.
所以问题是 - 有一种方法来声明一个指向const结构的指针,使用typedef的简写符号,或者必须总是一个特殊的单独声明:
So the question is - is there a way to declare a pointer to a const struct in a shorthanded notation with typedef, or there must always be a special separate declaration for it:
typedef const struct some_struct *pcSomeStruct;
void someFunction2( pcSomeStruct var )
推荐答案
,do not typedef pointers:)
Basically, do not typedef pointers :)
typedef struct some_struct {} some_struct;
void some_function1(some_struct *var);
void some_function2(const some_struct *var);
void some_function3(some_struct *const var);
void some_function4(const some_struct *const var);
或者,完全不要typedef:D
Or, don't typedef at all :D
struct some_struct {};
void some_function1(struct some_struct *var);
void some_function2(const struct some_struct *var);
void some_function3(struct some_struct *const var);
void some_function4(const struct some_struct *const var);
这篇关于简写typedef指向常量结构体的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!