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

发布于 2024-11-19 01:03:27 字数 568 浏览 0 评论 0原文

使用 typedef 声明一个结构体

typedef struct some_struct {
int someValue;
} *pSomeStruct;

,然后将其作为参数传递给某个使用 const 声明的函数,这意味着 'const some_struct * var'

void someFunction1( const pSomeStruct var )

结果变成

some_struct * const var

这在 ISO C 标准第 6.7.5.1 节中也有说明,该标准规定 'const'在这种情况下适用于指针而不是它指向的数据。

所以问题是 - 有没有办法用 typedef 以简写表示法声明指向 const 结构的指针,或者必须始终有一个特殊的单独声明:

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

Declaring a struct with 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'

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.

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 )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼波传意 2024-11-26 01:03:27

基本上,不要 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);

或者,根本不要 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);

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

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);
迷你仙 2024-11-26 01:03:27

这些也是有效的:

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

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

不过,这种风格在 C++ 中并未广泛使用。相反:

struct some_struct {
    int someValue;
};

struct some_struct {
    int someValue;
};

然后进行单独的 typedef。但是,这也没有被广泛使用。许多其他 C++ 程序员和我宁愿声明像 void foo (some_struct const * const) 或类似的函数,除了反对指针的常见原因之外。

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. Rather:

struct some_struct {
    int someValue;
};

struct some_struct {
    int someValue;
};

and then do seperate typedefs. 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.

娇妻 2024-11-26 01:03:27

就我对 C 类型系统的理解而言,你不应该。首先 typedef 声明类型为“指向结构的指针”。对它应用 const 逻辑上会创建“指向结构的 const 指针”。将常量注入结构本身需要更改类型本身,因此需要重新定义类型。
AFAIK,C 中的类型组合是增量的,您只能通过将修饰符应用于现有类型来创建新类型,而不能注入。

无论如何,指针的 Typedef 风格都不是很好。

As far as my understanding of C type system goes, you should not. First typedef declares type that reas as "pointer to the struct". Applying const to it logically creates "const pointer to the struct". Injecting const'ness to the struct itself would require changing type itself and, so it goes, retypedef'ing.
AFAIK, type composition in C is incremental and you can only make new types by applying modifiers to existing ones, not injecting.

Typedef of a pointer in not a good style anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文