简体   繁体   English

指向常量结构的速记 typedef 指针

[英]shorthand typedef pointer to a constant struct

Declaring a struct with typedef使用 typedef 声明结构

typedef struct some_struct {
int someValue;
} *pSomeStruct;

and then passing it as a parameter to some function with const declaration, implying 'const some_struct * var'然后将其作为参数传递给带有 const 声明的 function,暗示 'const some_struct * var'

void someFunction1( const pSomeStruct var )

turns out to become结果变成

some_struct * const var

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. ISO C 标准的第 6.7.5.1 节也说明了这一点,该标准指出,在这种情况下,“const”适用于指针,而不适用于它指向的数据。

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 结构的指针,或者必须始终有一个特殊的单独声明:

typedef const struct some_struct *pcSomeStruct;
void someFunction2( pcSomeStruct var )

Basically, do not typedef pointers:)基本上,不要 typedef 指针:)

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);

Or, don't typedef at all:D或者,根本不要 typedef: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);

Those are valid, too:这些也是有效的:

typedef struct some_struct {
    int someValue;
} const * pSomeStruct;

typedef struct some_struct {
    int someValue;
} const * const pSomeStruct;

This style is not used widely in C++, though.不过,这种风格在 C++ 中并未广泛使用。 Rather:相当:

struct some_struct {
    int someValue;
};

struct some_struct {
    int someValue;
};

and then do seperate typedefs.然后做单独的typedef。 But then, that's not widely used as well.但是,这也没有被广泛使用。 Many other C++ programmers and me would rather declare your functions like void foo (some_struct const * const) or similar, apart from the usual reasons against pointers.许多其他 C++ 程序员和我宁愿声明你的函数,如void foo (some_struct const * const)或类似的,除了反对指针的通常原因。

As far as my understanding of C type system goes, you should not.据我对 C 类型系统的理解,你不应该。 First typedef declares type that reas as "pointer to the struct".第一个 typedef 声明类型为“指向结构的指针”。 Applying const to it logically creates "const pointer to the struct".对它应用 const 在逻辑上会创建“指向结构的 const 指针”。 Injecting const'ness to the struct itself would require changing type itself and, so it goes, retypedef'ing.将 const'ness 注入结构本身将需要更改类型本身,因此需要重新定义类型。 AFAIK, type composition in C is incremental and you can only make new types by applying modifiers to existing ones, not injecting. AFAIK,C 中的类型组合是增量的,您只能通过将修饰符应用于现有类型而不是注入来创建新类型。

Typedef of a pointer in not a good style anyway.无论如何,指针的 typedef 都不是很好的样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM