简体   繁体   English

为什么这个TypeType指针指向一个结构体

[英]why is this pointer Typedef'd to a struct

I've been looking through the code for various IPC systems and I happen to come across these lines in Mach(xnu-123.5/osfmk/ipc/ipc_port.h) 我一直在浏览各种IPC系统的代码,碰巧在Mach(xnu-123.5 / osfmk / ipc / ipc_port.h)中遇到了这些问题。

typedef port_name_t          mach_port_name_t;
typedef mach_port_name_t    *mach_port_name_array_t;

where the definition of port_name_t is a struct. 其中port_name_t的定义是一个结构。

I have yet to come across any usage of *mach_port_name_array_t and was just a little surprised to see a pointer typedef'd to a struct, any insight into why this was done? 我还没有遇到过*mach_port_name_array_t任何用法,看到一个类型为结构体的指针类型感到有点惊讶,对为什么这样做有任何见解? or just some sample code showing *mach_port_name_array_t in action would be much appreciated. 或者只是一些示例代码显示*mach_port_name_array_t的实际效果,将不胜感激。

I [...] was just a little surprised to see a pointer typedef'd to a struct 我[...]有点惊讶地看到一个类型为结构体的指针

That's not how I would describe the declaration you presented. 那不是我要描述您提出的声明的方式。 Consider the code 考虑代码

 typedef mach_port_name_t *mach_port_name_array_t; 

If the typedef keyword were absent then it would declare mach_port_name_array_t as a pointer to an object of type mach_port_name_t , which happens to be a structure type. 如果没有typedef关键字,则它将声明mach_port_name_array_t作为指向类型为mach_port_name_t的对象的指针,该对象恰好是结构类型。 Pointers to structures are utterly routine in C, to the extent that C has a built-in operator ( -> ) for accessing structure members via a pointer. 结构的指针在C中完全是例程,因为C具有内置的运算符( -> ),用于通过指针访问结构成员。

The addition of typedef simply makes the declared identifier ( mach_port_name_array_t ) a type alias for that type, instead of an object of that type. 添加typedef只会使声明的标识符( mach_port_name_array_t )成为该类型的类型别名,而不是该类型的对象。 You can do this to create a type alias for any type, so creating an alias for a pointer-to-structure type is perfectly valid. 您可以执行此操作以为任何类型创建类型别名,因此为指向结构的指针类型创建别名是完全有效的。

On the other hand, although creating such type aliases is not so uncommon, it is poor style. 另一方面,尽管创建这种类型别名的情况并不少见,但风格很差。 In most cases, typedef s that hide a type's pointer nature produce more confusion than clarity. 在大多数情况下,隐藏类型指针性质的typedef造成的混乱比清楚还多。 Naming conventions can help with that, but not as much as avoiding such typedefs altogether does. 命名约定可以帮助您解决问题,但不能完全避免此类typedef。

Furthermore, in this particular case, the name is misleading. 此外,在这种特殊情况下,该名称具有误导性。 It suggests that the type is an array type, when it is not. 它暗示该类型是数组类型,而不是数组类型。 It is, however, the correct pointer type for a function parameter whose actual argument is expected to be an array: 但是,对于实际参数预期为数组的函数参数,它是正确的指针类型:

void do_something_with_ports(mach_port_name_array_t port_names);

// ...

mach_port_name_t ports[3];

// ...

do_something_with_ports(ports);

As I said, however, using a typedef'd pointer type is poor style. 但是,正如我所说,使用类型定义的指针类型是较差的样式。 The function would be better declared like so: 最好像下面这样声明该函数:

void do_something_with_ports(mach_port_name_t *port_names);

or, equivalently, 或者,等效地,

void do_something_with_ports(mach_port_name_t port_names[]);

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

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