INT国米pretation(*一)[3] [英] Interpretation of int (*a)[3]

查看:152
本文介绍了INT国米pretation(*一)[3]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在C数组和指针的工作,你会很快发现,他们是绝不等同尽管它可能在第一眼看上去如此。我知道在L-值和R值的差异。不过,最近我试图找出我可以结合一个二维数组,即使用指针的类型。

  INT富[2] [3];
INT(*一)[3] = foo的;

不过,我无法找出编译器如何理解的类型定义,尽管正规运营商precedence规则 * [] 。相反,如果我是用一个typedef,问题就变得简单显著:

  INT富[2] [3];
的typedef INT my_t [3];
my_t * A = foo的;

在底线,有人可以回答我的问题是如何术语 INT(*一)[3] 是由编译器读取? int类型的[3] 很简单,为int * A [3] 也很简单。但随后,为什么不为int *(A [3])

编辑:当然,而不是强制转换我的意思是类型定义(这只是一个错字)


解决方案

首先,你的意思是类型定义,在你的问题不是强制转换。

在C,指针键入 T 可以指向类型的对象 T

  INT * PI;
INT I;
PI =安培;我;

以上是简单易懂。现在,让我们使它更复杂一点。你似乎知道数组和指针之间的差值(即,你知道数组不是指针,它们的行为就像他们有时虽然)。所以,你应该能够理解:

  int类型的[3];
为int * PA = A;

但出于完整性的缘故:在分配,名称 A 等同于&放大器;一个[0] ,即一个指向数组 A 的第一个元素。如果你不知道如何以及为什么这个工程中,有很多答案正是说明在数组衰变的指针的名称,当它没有:


  • 我的回答题目的问题类型数组

  • 另一个<一个href=\"http://stackoverflow.com/questions/2036096/literal-string-initializer-for-a-character-array/2036125#2036125\">answer举例实例时,数组的名称不衰减到一个指针,而

  • 的答案 是什么阵列腐烂

我相信有这么多更多这样的问题和答案,我只是提到了一些,我从一个搜索中找到。

回到正题:当我们有:

  INT富[2] [4];

的类型是数组 [2] 阵列 [3] INT 。这意味着 foo的[0] 是3 INT 秒的阵列,而富[ 1] 是一个数组3 INT 秒。

现在假设我们要声明一个指针,我们要赋值给 foo的[0] 。也就是说,我们想要做的:

  / *不知何故声明P * /
P = foo的[0];

以上是在形式上没有什么区别为int * PA = A; 行,因为类型 A 和的foo [0] 是相同的。因此,我们需要 INT * P; 作为我们的声明 P

现在,要记住阵列最主要的是,规则有关数组的名字腐烂的指针它的第一个元素仅适用一次。如果你有一个数组的数组,然后在值上下文中,数组的名字将不会衰减到类型为指针指针,而是以指针数组。让我们回到

  / *应该是什么q的类型? * /
Q = foo的;

名称上面是一个指向的第一个元素,也就是说,我们可以写上如:

  Q =&放大器;富[0];

类型的foo [0] 是数组 [3] INT 。因此,我们需要是一个指针数组 [3] INT

  INT(* Q)[3];

需要围绕括号,因为 [] 结合更紧密* 在C,所以为int * q [3] 声明为指针数组,我们希望有一个指向数组的指针。 为int *(Q [3])是,从上面,相当于为int * Q [3] ,也就是说, 3指针数组 INT

希望有所帮助。你也应该阅读℃,聪明豆:数组和指针的关于这一主题的一个很好的教程

关于阅读常规声明:你看过他们由内向外,与变量的名称开始(如果有的话)。你离开去尽可能除非有一个 [] 眼前的权利,而你总是孝敬括号。 CDECL 应该能够帮助你的程度:

  $ CDECL
CDECL&GT;申报数p作为指针为int数组3
INT(* P)[3]
CDECL&GT;解释INT(* P)[3]
申报数p作为指针为int数组3

要读

  INT(*一)[3];      一个#一个是
    (*)#圆括号,所以precedence变化。
                   #指针
        [3]#的阵列[3]的
INT; #INT。

有关

 为int * a [3];     一个#一个是
      [3]#的阵列[3]的
    *#不能去正确的,所以去左边。
                   #指针
INT; #INT。

有关

 的char *(*(* A [])())()          一个#一个是
           []#数组
         *#指针
        ()()#功能采取的参数数目不详
      (*)#和返回指针为
                 ()#功能
字符*#返回字符指针

(从 C-FAQ问题1.21 的例子。在实践中,如果你正在阅读这样一个复杂的宣言,也有一些是严重错误code!)

When working with arrays and pointers in C, one quickly discovers that they are by no means equivalent although it might seem so at a first glance. I know about the differences in L-values and R-values. Still, recently I tried to find out the type of a pointer that I could use in conjunction with a two-dimensional array, i.e.

int foo[2][3];
int (*a)[3] = foo;

However, I just can't find out how the compiler "understands" the type definition of a in spite of the regular operator precedence rules for * and []. If instead I were to use a typedef, the problem becomes significantly simpler:

int foo[2][3];
typedef int my_t[3];
my_t *a = foo;

At the bottom line, can someone answer me the questions as to how the term int (*a)[3] is read by the compiler? int a[3] is simple, int *a[3] is simple as well. But then, why is it not int *(a[3])?

EDIT: Of course, instead of "typecast" I meant "typedef" (it was just a typo).

解决方案

First, you mean "typedef" not "typecast" in your question.

In C, a pointer to type T can point to an object of type T:

int *pi;
int i;
pi = &i;

The above is simple to understand. Now, let's make it a bit more complex. You seem to know the difference between arrays and pointers (i.e., you know that arrays are not pointers, they behave like them sometimes though). So, you should be able to understand:

int a[3];
int *pa = a;

But for completeness' sake: in the assignment, the name a is equivalent to &a[0], i.e., a pointer to the first element of the array a. If you are not sure about how and why this works, there are many answers explaining exactly when the name of an array "decays" to a pointer and when it does not:

I am sure there are many more such questions and answers on SO, I just mentioned some that I found from a search.

Back to the topic: when we have:

int foo[2][4];

foo is of type "array [2] of array [3] of int". This means that foo[0] is an array of 3 ints, and foo[1] is an array of 3 ints.

Now let's say we want to declare a pointer, and we want to assign that to foo[0]. That is, we want to do:

/* declare p somehow */
p = foo[0];

The above is no different in form to the int *pa = a; line, because the types of a and of foo[0] are the same. So, we need int *p; as our declaration of p.

Now, the main thing to remember about arrays is that "the rule" about array's name decaying to a pointer to its first element applies only once. If you have an array of an array, then in value contexts, the name of the array will not decay to the type "pointer to pointer", but rather to "pointer to array". Going back to foo:

/* What should be the type of q? */
q = foo;

The name foo above is a pointer to the first element of foo, i.e., we can write the above as:

q = &foo[0];

The type of foo[0] is "array [3] of int". So we need q to be a pointer to an "array [3] of int":

int (*q)[3];

The parentheses around q are needed because [] binds more tightly than * in C, so int *q[3] declares q as an array of pointers, and we want a pointer to an array. int *(q[3]) is, from above, equivalent to int *q[3], i.e., an array of 3 pointers to int.

Hope that helps. You should also read C for smarties: arrays and pointers for a really good tutorial on this topic.

About reading declarations in general: you read them "inside-out", starting with the name of the "variable" (if there is one). You go left as much as possible unless there is a [] to the immediate right, and you always honor parentheses. cdecl should be able to help you to an extent:

$ cdecl
cdecl> declare p as  pointer to array 3 of int
int (*p)[3]
cdecl> explain int (*p)[3]
declare p as pointer to array 3 of int

To read

int (*a)[3];

      a            # "a is"
    (* )           # parentheses, so precedence changes.
                   # "a pointer to"
        [3]        # "an array [3] of"
int        ;       # "int".

For

int *a[3];

     a             # "a is"
      [3]          # "an array [3] of"
    *              # can't go right, so go left.
                   # "pointer to"
int      ;         # "int".

For

char *(*(*a[])())()

          a         # "a is"
           []       # "an array of"
         *          # "pointer to"
        (    )()    # "function taking unspecified number of parameters"
      (*        )   # "and returning a pointer to"
                 () # "function"
char *              # "returning pointer to char"

(Example from c-faq question 1.21. In practice, if you are reading such a complicated declaration, there is something seriously wrong with the code!)

这篇关于INT国米pretation(*一)[3]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆