编译器如何理解指针和数组的声明[关闭](How does compiler understand the declaration of pointers and arrays [closed])

编程入门 行业动态 更新时间:2024-10-26 14:28:19
编译器如何理解指针和数组的声明[关闭](How does compiler understand the declaration of pointers and arrays [closed])

当我被指针指向数组和指针数组混淆时,我想到了这个问题,尽管我已经想出了如何区分它们,从字面上看。

但我仍然对编译器如何理解指针声明感到困惑。 例如,有没有像int*这样的类型?

int *p; // a common pointer declaration

具体来说,编译器如何理解前一句话? 首先将p作为指针处理,然后找到指向的对象是int ? 或者发现用户声明了一个指向int的指针int*名为p ?

面对数组指针时,我更容易混淆。

int (*p)[4] //a pointer to an array of int[4]

怎么理解这个? 编译器是否将其视为int[4] *p ( int[4]工作方式类似于我们在容器中的新类型)? 以下案例中的类似问题。

int *p[4] //an array-of-pointers

由于[]在*之前,编译器是否首先理解p[4]并将p视为数组(具有未知元素类型),然后将元素类型指定为int* ?

The question comes to my mind when I was confused by pointer-to-arrays and arrays-of-pointers though I've figured out how to distinguish them now, literally.

But I'm still puzzled with how compiler understand the declaration of pointers. For example, is there any type like int*?

int *p; // a common pointer declaration

Specifically, how does compiler understand the former sentence? Treat p as a pointer first and then find the object pointed is int? Or finding user declared a pointer-to-int int* named p?

It's more confusing to me when facing pointer-to-arrays.

int (*p)[4] //a pointer to an array of int[4]

How does understand this? Does compiler treat this as int[4] *p(int[4] works like a new type like how we do in containers)? Similar questions in following case.

int *p[4] //an array-of-pointers

As [] is prior to *, does compiler understand p[4] first and treat p as an array (with unknown elements type), then specify the elements type to int*?

最满意答案

声明的解析可以在当前标准的第6.7.6节中找到。 它太大而不能完全过去,但简而言之,关于p类型的规则是以归纳的方式进行的。 在这些规则中, T是普通类型(没有指针/数组等), D是声明符:

T * D被定义为如果TD的D的类型将是“某些/ T ”,则D的类型是“指向T的指针”。

TD[N] (其中N可以是空白或其他各种东西)被定义为如果TD的D的类型将是“ T东西”,则D的类型是“某些数组(维数) N )of T “。

因此,您可以看到每个规则修改了规则的先前应用程序的结果,直到我们进入归纳的“结束”,当D是普通标识符时发生。

此外, T ( D )表示除强制解析之外的TD 。

有些消息来源将声明描述为“从里到外”,虽然这个感应链实际上是“在外面”,但你需要进入然后回溯你的结果。 虽然最终结果是相同的,但我们可能被教导作为人类阅读的方式与语言定义不同。


要使用你的一个例子, int *p[4] :

规则1 - 其形式为T * D , T = int且D = p[4] 。 由于TD将是“int的数组[4]”(见下文),因此T * D是“指向int的指针的数组[4]”。

在这一步中,我们分析了int q[4] :

规则2 - 其形式为TD[N] ,其中T = int且D = q 。 这是“结束案例”,因为D是普通标识符,因此此步骤的q类型是“ int数组[4]”。

再举一个例子, int (*p)[4]:

规则2 - 这是TD[N]的形式,其中T = int , D = (*p) 。 由于TD将是“指向int指针”,因此TD[4]是“指向int数组[4]的指针”。

The parsing of declarations can be found in section 6.7.6 of the current standard. It's too big to go over in full, but in brief the rules about the type of p are laid out in inductive manner. In these rules T is a plain type (no pointers/arrays etc.) and D is a declarator:

T * D is defined to mean that if the type of D in T D would be "something of/to T", then the type of D is "something of pointer to T".

T D[N] (where N could be blank or various other things) is defined to mean that if the type of D in T D would be "something of T", then the type of D is "something of/to array (of dimension N) of T".

So you can see each rule modifies the result of a previous application of the rule, until we get down to the "end" of the induction which happens when D is a plain identifier.

Also, T ( D ) means T D other than enforcing parsing.

Some sources describe declarations as being read "inside out", although this induction chain actually occurs "outside in", but you need to go in and then backtrack with your results. The way we may be taught to read as humans differs from the language definition, although the end result is the same.


To use one of your examples, int *p[4]:

Rule 1 - this has the form T * D, with T = int and D = p[4]. Since T D would be "array[4] of int" (see below), then T * D is "array[4] of pointer to int".

In this step we analyzed int q[4]:

Rule 2 - this has the form T D[N], with T = int and D = q. This is the "end case" because D is a plain identifier, so the type of q at this step is "array[4] of int".

For another example, int (*p)[4]:

Rule 2 - this is of the form T D[N], where T = int, and D = (*p). Since T D would be "pointer to int", then T D[4] is "pointer to array[4] of int".

更多推荐

本文发布于:2023-07-09 14:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1086990.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编译器   数组   指针   声明   compiler

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!