难道一个数组变量指向自己? [英] Does an Array variable point to itself?

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

问题描述

我尝试了一些code,检查数组和指针的行为。其如下:

I tried some code to check the behavior of array and pointers. Its as follows.

#include <stdio.h>
main(){
int s[]={1,2};
int *b=s;
printf("%d, %d, %d\n", s, &s, *s);
printf("%d, %d, %d\n", b ,&b, *b);
}

起初我以为指针和阵列是相同的,但...结果
令我惊讶的's'和值'和氏'不像'B'一样。这是否意味着数组变量指向本身?

Initially I thought of pointers and array to be same BUT...
To my surprise the value of 's' and '&s' are SAME unlike 'b'. Does that mean an Array variable "points to itself?"

我现在还什么实际上是一个变量名糊涂?其结合是如何发生的,在内存中的位置?我只是无法想象的事,去在那里!
而在内存(RAM)是存储在运行时的所有信息?

I am also now confused with what actually is a variable "name"? How its binding takes place with a location in memory? I am just unable to visualize things that go on there! And where in memory (RAM) is all the information stored at run time?

推荐答案

除了当它的操作数的的sizeof 或一元&放大器; 运营商,或者正在使用字符串文字来初始化声明另一个数组,键入 T N个元素的数组的前pression 指针将与类型的前pression改为 T ,和前pression的价值将是第一个地址元件阵列中的

Except when it is the operand of the sizeof or unary & operators, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T" will be replaced with an expression of type "pointer to T", and the value of the expression will be the address of the first element in the array.

假设以下code:

int arr[10];
foo(arr);

在调用函数,前pression 改编从类型转换10 INT 到指针 INT ,并且第一个元素的地址<$ C的-element阵列$ C>改编是实际被传递给

In the call to the function foo, the expression arr is converted from type "10-element array of int" to "pointer to int", and the address of the first element of arr is what actually gets passed to foo.

我们将定义作为任何

void foo(int a[]) {}

void foo(int *a) {}

在函数参数声明的情况下, T一[] T一[N] 是相同的到 T * A ;该参数是一个的指针的类型,而不是数组类型。注意,这是的只有的为函数参数声明真实的。

In the context of a function parameter declaration, T a[] and T a[N] are identical to T *a; the parameter is a pointer type, not an array type. Note that this is only true for function parameter declarations.

正如上面提到的,例外是当数组前pression是一元&放大器的操作; 运营商。如果我们改变调用

As mentioned above, one exception to this rule is when the array expression is the operand of the unary & operator. If we change the call to foo to read

foo(&arr);

然后是前pression类型&放大器;改编是指向的10个元素的数组 INT ,或 INT(*)[10] ,和前pression的值为 A 。为此,的定义是

then the type of the expression &arr is "pointer to 10-element array of int", or int (*)[10], and the value of the expression is the address of a. For this, the definition of foo would be

void foo(int (*a)[10]) {}

在C,数组的地址和数组的第一个元素的地址都是一样的 - 因此无论是前pressions的改编&放大器;改编具有相同的的,但它们的类型是不同的。这会影响涉及到指针算术运算。例如,假设我们code被写了

In C, the address of the array and the address of the first element of the array are the same - thus both of the expressions arr and &arr have the same value, but their types are different. This matters for operations involving pointer arithmetic. For example, assume our code had been written

int arr[10];
foo(arr);
...
void foo(int *a)
{
   ...
   a++;  
   ...
}

在项, A 改编[0] 。这位前pression A ++ 将推进指针指向数组(下一个整数改编[1] )。

On entry, a points to arr[0]. The expression a++ would advance the pointer to point to the next integer in the array (arr[1]).

现在假设code已被写成

Now assume the code had been written as

int arr[10];
foo(&arr);
...
void foo(int (*a)[10])
{
  ...
  a++;
  ...
}

在项, A 仍然指向改编[0] (记住,数组的地址是相同数组的第一个元素)的地址,但此时前pression A ++ 将提前指针指向下一个的 10元素的数组的整数;而不是推进指针的sizeof(INT)字节,我们提前就的sizeof(INT [10])字节。

On entry, a still points to arr[0] (remember, the address of the array is the same as the address of the first element of the array), but this time the expression a++ will advance the pointer to point to the next 10-element array of integers; instead of advancing the pointer sizeof (int) bytes, we advance it sizeof (int[10]) bytes.

所以这就是为什么在你的的printf 语句你看到同样的的两个取值&放大器; S 。您应该使用%P 转换指定打印指针值,并预计相应参数的类型为无效* ,所以改变那些的printf 语句

So this is why in your printf statement you see the same values for both s and &s. You should use the %p conversion specifier to print pointer values, and it expects the corresponding argument to be type void *, so change those printf statements to

printf("%p %p %d\n", (void *) s, (void *) &s, *s);
printf("%p %p %d\n", (void *) b, (void *) &b, *b);

这篇关于难道一个数组变量指向自己?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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