是一个数组名在C中的指针? [英] Is an array name a pointer in C?

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

问题描述

是一个数组的名字在C中的指针?
如果不是这样,一个阵列的姓名和指针变量之间的差是什么?

Is an array's name a pointer in C? If not, what is the difference between an array's name and a pointer variable?

推荐答案

数组是一个数组和指针是一个指针,但在大多数情况下,数组名的转换的的指针。经常使用的术语,就是他们的衰减的的指针。

An array is an array and a pointer is a pointer, but in most cases array names are converted to pointers. A term often used is that they decay to pointers.

下面是一个数组:

int a[7];

包含空格七个整数,你可以把一个价值在其中的一个与分配,是这样的:

a contains space for seven integers, and you can put a value in one of them with an assignment, like this:

a[3] = 9;

下面是一个指针:

int *p;

P 不包含任何整数位,但它可以指向一个整数的空间。我们可以,例如,将其设置为指向数组中的地方之一 ,如第一个:

p doesn't contain any spaces for integers, but it can point to a space for an integer. We can, for example, set it to point to one of the places in the array a, such as the first one:

p = &a[0];

有什么可以令人困惑的是,你还可以这样写:

What can be confusing is that you can also write this:

p = a;

这确实的的复制数组的内容, 到指针 P (不管这将意味着)。取而代之的是,数组名称的被转换成一个指针到它的第一个元素。这样分配并同为previous之一。

This does not copy the contents of the array a into the pointer p (whatever that would mean). Instead, the array name a is converted to a pointer to its first element. So that assignment does the same as the previous one.

现在,您可以使用p在以类似的方式数组:

Now you can use p in a similar way to an array:

p[3] = 17;

这此工作的原因是,该阵列用C解除引用运算符,[],在指针定义。 X [Y] 的意思是:先从指针的 X ,步元素是什么指针指向后向前,再采取任何有。使用指针运算语法的 X [Y] 也可以写成 *(X + Y)

The reason that this works is that the array dereferencing operator in C, "[ ]", is defined in terms of pointers. x[y] means: start with the pointer x, step y elements forward after what the pointer points to, and then take whatever is there. Using pointer arithmetic syntax, x[y] can also be written as *(x+y).

有关这与正常的阵列,比如我们的 ,名工作 [3] 必须首先转换一个指针(第一个元素在 )。然后,我们第3步向前的元素,并采取一切是存在的。换句话说:取元件在阵列中的位置3。 (这是阵列中的第四元件中,由于第一个被编号为0)。

For this to work with a normal array, such as our a, the name a in a[3] must first be converted to a pointer (to the first element in a). Then we step 3 elements forward, and take whatever is there. In other words: take the element at position 3 in the array. (Which is the fourth element in the array, since the first one is numbered 0.)

所以,综上所述,在C程序中的数组名(在大多数情况下)转换为指针。一个例外是,当我们使用的sizeof 运营商的阵列上。如果 转化为一个指针这样的大赛,的sizeof(A)将给予,而不是实际的数组的指针,这将是相当无用的大小,因此在这种情况下, 表示数组本身。

So, in summary, array names in a C program are (in most cases) converted to pointers. One exception is when we use the sizeof operator on an array. If a was converted to a pointer in this contest, sizeof(a) would give the size of a pointer and not of the actual array, which would be rather useless, so in that case a means the array itself.

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

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