数组是指针? [英] Arrays are Pointers?
问题描述
可能重复:结果
是数组名在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
;它指的是内存足够大的连续块存储10INT
值。该的前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 thesizeof
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 ofint
; it refers to a contiguous block of memory large enough to store 10int
values. The expressionarr
in the second declaration is of array type, but since it is not the operand of&
orsizeof
and it isn't a string literal, the type of the expression becomes "pointer toint
", 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 singleint
object. It is initialized to point to the first element inarr
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 0x00The types matter for things like
sizeof
and&
;sizeof arr == 10 * sizeof (int)
, which in this case is 20, whereassizeof parr == sizeof (int *)
, which in this case is 4. Similarly, the type of the expression&arr
isint (*)[10]
, or a pointer to a 10-element array ofint
, whereas the type of&parr
isint **
, or pointer to pointer toint
.Note that the expressions
arr
and&arr
will yield the same value (the address of the first element inarr
), but the types of the expressions are different (int *
andint (*)[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 plussizeof (int)
), and0x10008014
(base plussizeof (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 expressionarr
is not appearing as an operand of eithersizeof
or&
, and since it is not a string literal being used to initialize another array, its type is converted from "10-element array ofint
" to "pointer toint
", 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 ofint
, whereasfoo
is dealing with a pointer toint
, 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 andi
is an integer expression,a[i]
andi[a]
are both valid expressions, and both will yield the same value.这篇关于数组是指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!