数组是指针? [英] Arrays are Pointers?

查看:140
本文介绍了数组是指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  是数组名在C中的指针?


是数组和指针的实现方式不同?我也碰到过这个问题,因为,在这两个我们从element.So的起始地址访问元素的情况下,应该有他们之间的密切关系。请解释一下它们之间的确切关系。谢谢你。


解决方案

让我们重要的东西出来的第一方式:数组不是指针。数组类型和指针类型的完全不同的事情的并且是由编译器区别对待。

在哪里产生了困惑与C如何对待数组的前pressions 的。 N1570


6.3.2.1左值,数组和功能指示器


...结果
3除非它是的sizeof 运算符的操作数,则 _Alignof 运营商,或
一元运营商,或者是用于初始化数组,有一个前pression一个字符串文字
类型'的的数组类型的''转换为类型的前pression''指针的键入的''那点
到数组对象,初始元素不是一个左值。如果数组对象
寄存器存储类,行为是理解过程网络定义。

让我们来看看下面的声明:

  INT改编[10] = {0,1,2,3,4,5,6,7,8,9};
为int *帕尔=改编;

改编是一个10个元素的数组 INT ;它指的是内存足够大的连续块存储10 INT 值。该的前pression 改编在第二个声明是数组类型,但因为它不是的操作数&安培; 的sizeof ,它不是一个字符串文字,在前pression类型的变指针 INT 和值是第一个元素的地址,或&放大器;常用3 [0]

帕尔为int的指针;它指的是内存足够大的块持单 INT 对象的地址。它被初始化为指向第一元件在改编如上所述。

下面是示出两个(假定16位整数和32位地址)之间的关系的假想存储器映射


对象地址为0x00为0x01 0X02×03
------ ------- ----------------------
   ARR 0x10008000 0×00 0×00 0×00 0×01
                 0x10008004 0×00 0×02 0×00×03
                 0x10008008 0×00 0×04 0×00 0×05
                 0x1000800c 0×00 0×06 0×00 0×07
                 0x10008010为0x00 0x08的0×00×09
  帕尔0x10008014为0x10 0x80的0×00 0×00

该种物质的东西像的sizeof &安培; ; sizeof的改编== 10 * sizeof的(INT),在这种情况下是20,而 sizeof的帕尔==的sizeof(INT *),在这种情况下是4同样,前pression &放大器的类型; ARR INT(*) [10] ,或指向 INT 的有10个元素的数组,而类型&放大器;帕尔 INT ** ,或指针的指针的 INT

请注意,我们的前pressions 改编&放大器;改编将产生相同的的(第一个元素的改编的地址),但类型前pressions是不同的(为int * INT(*)[10] ,分别)。使用指针运算时,会产生一些差异。例如,给出:

  INT改编[10] = {0,1,2,3,4,5,6,7,8,9};
为int * p =改编;
INT(* AP)[10] =安培;改编;的printf(前:ARR =%P,P =%P,AP =%P \\ N(无效*)改编,(无效*)P,(无效*)AP);
p ++;
AP ++;
的printf(之后:ARR =%P,P =%P,AP =%P \\ N(无效*)改编,(无效*)P,(无效*)AP);

之前行应打印相同的值对所有三个前pressions(在我们的假想图, 0x10008000 )。 本经行应显示三个不同值: 0x10008000 0x10008002 (基地+ 的sizeof (INT))和 0x10008014 (基地+ 的sizeof(INT [10]) )。

现在让我们回到上面第二段:阵列的前pressions 的是在大多数情况下转换为指针类型。让我们来看看标前pression 改编[I] 。由于前pression 改编未出现作为一个操作数为的sizeof &放大器; ,并且因为它不是字面被用于初始化另一个阵列的字符串,其类型由 INT 10元素数组转换到指针到 INT ,下标操作被应用到这个的指针的值。事实上,当你看看C语言定义,您将看到以下文字:


6.5.2.1数组下标的结果
...结果
2一个后缀前pression随后在方括号中前pression [] 是一个数组对象的元素的下标名称。下标操作的定义的 [] 是该的E1 [E2] 是相同的(*((E1)+(E2)))的。因为这适用于二元+运算符,如转换规则的E1 是一个数组对象(等同于一个指针数组对象的初始元素)和 E2 是一个整数, E1 [E2] 指定在 E2 -th E1 (从零算起)的元素。

在实践中,这意味着你可以使用下标运算符的指针对象的如同的它是一个数组。这就是为什么code像

  INT美孚(INT * P,为size_t大小)
{
  INT总和= 0;
  INT I;
  对于(i = 0; I<大小;我++)
  {
    总和+ = P [I]
  }
  返回总和;
}INT主要(无效)
{
  INT ARR [10] = {0,1,2,3,4,5,6,7,8,9};
  INT结果= foo的(ARR,sizeof的ARR / ARR的sizeof [0]);
  ...
}

工作它的方式。 正在处理 INT 的数组,而正在处理一个指向 INT ,但两者都能够使用下标运算符的如同的他们都处理数组类型。

这也意味着数组下标是的可交换的:假设 A 是一个数组前pression和 I 是一个整数前pression, A [I] I [A] 都是有效的前pressions,两个会产生相同的值。

Possible Duplicate:
Is array name a pointer in C?

Are arrays and pointers implemented differently? I have come across this question because , in both the cases we access elements from the starting address of an element.So , there should be close relation between them . Please explain the exact relation between them . Thanks.

解决方案

Let's get the important stuff out of the way first: arrays are not pointers. Array types and pointer types are completely different things and are treated differently by the compiler.

Where the confusion arises is from how C treats array expressions. N1570:

6.3.2.1 Lvalues, arrays, and function designators

...
3 Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

Let's look at the following declarations:

int arr[10] = {0,1,2,3,4,5,6,7,8,9};
int *parr = arr;

arr is a 10-element array of int; it refers to a contiguous block of memory large enough to store 10 int values. The expression arr in the second declaration is of array type, but since it is not the operand of & or sizeof and it isn't a string literal, the type of the expression becomes "pointer to int", and the value is the address of the first element, or &arr[0].

parr is a pointer to int; it refers to a block of memory large enough to hold the address of a single int object. It is initialized to point to the first element in arr as explained above.

Here's a hypothetical memory map showing the relationship between the two (assuming 16-bit ints and 32-bit addresses):

Object           Address         0x00  0x01  0x02  0x03
------           -------         ----------------------
   arr           0x10008000      0x00  0x00  0x00  0x01
                 0x10008004      0x00  0x02  0x00  0x03
                 0x10008008      0x00  0x04  0x00  0x05
                 0x1000800c      0x00  0x06  0x00  0x07
                 0x10008010      0x00  0x08  0x00  0x09
  parr           0x10008014      0x10  0x00  0x80  0x00

The types matter for things like sizeof and &; sizeof arr == 10 * sizeof (int), which in this case is 20, whereas sizeof parr == sizeof (int *), which in this case is 4. Similarly, the type of the expression &arr is int (*)[10], or a pointer to a 10-element array of int, whereas the type of &parr is int **, or pointer to pointer to int.

Note that the expressions arr and &arr will yield the same value (the address of the first element in arr), but the types of the expressions are different (int * and int (*)[10], respectively). This makes a difference when using pointer arithmetic. For example, given:

int arr[10] = {0,1,2,3,4,5,6,7,8,9};
int *p = arr;
int (*ap)[10] = &arr;

printf("before: arr = %p, p = %p, ap = %p\n", (void *) arr, (void *) p, (void *) ap);
p++;
ap++;
printf("after: arr = %p, p = %p, ap = %p\n", (void *) arr, (void *) p, (void *) ap);

the "before" line should print the same values for all three expressions (in our hypothetical map, 0x10008000). The "after" line should show three different values: 0x10008000, 0x10008002 (base plus sizeof (int)), and 0x10008014 (base plus sizeof (int [10])).

Now let's go back to the second paragraph above: array expressions are converted to pointer types in most circumstances. Let's look at the subscript expression arr[i]. Since the expression arr is not appearing as an operand of either sizeof or &, and since it is not a string literal being used to initialize another array, its type is converted from "10-element array of int" to "pointer to int", and the subscript operation is being applied to this pointer value. Indeed, when you look at the C language definition, you see the following language:

6.5.2.1 Array subscripting
...
2 A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero).

In practical terms, this means you can apply the subscript operator to a pointer object as though it were an array. This is why code like

int foo(int *p, size_t size)
{
  int sum = 0;
  int i;
  for (i = 0; i < size; i++)
  {
    sum += p[i];
  }
  return sum;
}

int main(void)
{
  int arr[10] = {0,1,2,3,4,5,6,7,8,9};
  int result = foo(arr, sizeof arr / sizeof arr[0]);
  ...
}

works the way it does. main is dealing with an array of int, whereas foo is dealing with a pointer to int, yet both are able to use the subscript operator as though they were both dealing with an array type.

It also means array subscripting is commutative: assuming a is an array expression and i is an integer expression, a[i] and i[a] are both valid expressions, and both will yield the same value.

这篇关于数组是指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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