为什么要显式引用指向结构数组的指针而不是仅仅通过指针?

Why reference a pointer to a struct array explicitly instead of just via pointer?

到目前为止,我总是这样访问一个指向结构数组的指针:

struct blah *ptr; ptr[10].member = 1;

我最近了解到另一种方式:

struct blah (*array_ptr)[]; (*array_ptr)[10].member = 1;

我发现的唯一限制是禁止array->member访问强制[]使用。

问题:对于 a) 人类或 b) 两种方法之间的编译器,显式 [] 方法是否有任何 pros/cons?

对于人类来说,我想它明确表示指针指向一个数组,而不是 'normal' 使用它是不明确的地方。

Up until now I have always accessed a pointer to a struct array thus:

struct blah *ptr; ptr[10].member = 1;

这演示了一个指向结构的指针(恰好是至少 11 个这样的结构的数组的元素),不是 指向结构数组的指针。但是,它是 struct blah 数组将衰减到的指针类型。

I recently became aware of another way:

struct blah (*array_ptr)[]; (*array_ptr)[10].member = 1;

是一个指向 [数量不详] 结构数组的指针。

这两个指针指向的地址可能相同,但指针类型不同

The only restriction I've found, is it prohibits array->member access forcing [] usage.

您可以使用任一形式间接访问 struct blah 对象。是的,由于指针类型不同,这两种情况的语法不同。

Question: Are there any pros/cons to the explicit [] method to either a) humans or b) compilers between either method?

我敢说大多数人发现结构指针变体比数组指针版本更容易阅读和理解。它对编译器应该没有任何区别,因为如果 array_ptr 指向第一个元素为 *ptr 的数组,并且该数组确实至少有 11 个元素,那么两个示例赋值是100% 等效。

For humans, I guess it makes it explicit that the pointer is to an array, as opposed to the 'normal' use where it is ambiguous.

一个指针可以总是被认为是指向一个数组,尽管它可能是一个长度为 1 的数组。尝试进行区分并没有特别的用处,在至少不是通过改变指针的类型。当你有指向已知长度数组的指针时(例如:struct blah (*array11_ptr)[11];),它会变得更有趣和更有用,但我没有看到你的 unspecified-length 版本有很多用例。