C语言中的array,& array和& array [0]有什么区别? [英] What's the difference among array, &array and &array[0] in C language?

查看:296
本文介绍了C语言中的array,& array和& array [0]有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我学习C语言中的数组和指针时,我很困惑,为什么ch,& ch,& ch [0]彼此相等,而sptr,& sptr,& sptr [0]不是? 这是我的源代码:

I've got confused when learning array and pointer in C language,why are ch, &ch,&ch[0] just equal to one another ,while sptr, &sptr,&sptr[0] are not? Here's my source code:

int main(void)
    {
        char ch[7] = { '1','2','3','4','5','6','0' };
        char *sptr = "132456";
        double db[4] = { 1.0,2.0,3.0,4.0 };
        printf("\n%p  %p  %p\n", ch, &ch,&ch[0]);
        printf("\n%p  %p  %p\n", sptr, &sptr,&sptr[0]);
        printf("\n%p  %p  %p\n", db, &db,&db[0]);
        return 0;

    }

我的机器上的输入是:

00FDFD68  00FDFD68  00FDFD68

00037CD0  00FDFD5C  00037CD0

00FDFD34  00FDFD34  00FDFD34

推荐答案

在大多数(尽管不是全部)上下文中,数组会衰减"到指向其第一个元素的指针中.这就是为什么示例中ch&ch[0]相同的原因(数组元素访问的优先级高于地址"运算符,因此后者也可以写为&(ch[0])).

In most (though not all) contexts an array "decays" into a pointer to its first element. This is why ch and &ch[0] are the same in your example (array element access has higher precedence than the "address of" operator, so the latter could also be written as &(ch[0])).

剩下的&ch是数组不衰减为指针的一种情况;相反,您获得了数组的地址.很自然,它与数组的第一个元素的地址相同-但是,重要的是,它具有不同的类型.它的类型为char (*)[7],即指向具有7个元素的char数组的指针.其他两个指针的类型为char *,即指向单个char的指针.

The remaining &ch is one case where the array does not decay into a pointer; instead, you get the address of the array. Naturally enough, this is the same as the address of the first element of the array - however, importantly, it has a different type; it is of type char (*)[7], i.e. a pointer to an array of char with 7 elements. The other two pointers are of type char *, i.e. a pointer to an individual char.

由于sptr是指针,所以&sptr是该指针的地址,并且自然会有所不同. &sptr[0]等效于sptr + 0,它当然等于sptr.

Since sptr is a pointer, &sptr is the address of that pointer and naturally will be different. &sptr[0] is equivalent to sptr + 0, which is of course equal to sptr.

您看不到为什么sptr&sptr产生不同的地址,这表明对指针是什么的误解.指针是一个固定大小的对象,其值可以引用(指向)某种特定类型的任意对象.因为它本身是一个对象,所以可以使一个指针指向另一个对象.另一方面,数组变量始终(在其生命周期内)都指向同一数组对象.

That you do not see why sptr and &sptr yield different addresses indicates a misunderstanding of what a pointer is. A pointer is a fixed-size object with a value that can refer to (point at) some arbitrary object of a particular type. Because it is an object itself, a pointer can be made to point at a different object. An array variable, on the other hand, always (during its lifetime) refers to the same array object.

在您的示例输出中:

00037CD0  00FDFD5C  00037CD0

第一个值00037CD0是sptr指向的位置-即,它是字符串常量"132456"在内存中的位置.第二个值00FDFD5C是sptr变量本身的地址.这表明在地址00FDFD5C处有一个指针对象,该指针对象的值为00037CD0.

The first value, 00037CD0, is the location to which sptr points - that is, it is the location in memory of the string constant "132456". The second value, 00FDFD5C, is the address of the sptr variable itself. What this shows is that there is a pointer object at address 00FDFD5C which holds the value 00037CD0.

本质上,这两种情况之间的区别归结为:

Essentially, the difference between the two cases boils down to this:

  • 数组的地址与其第一个元素的地址相同
  • 另一方面,指针的地址与指针当前指向的内容无关.

这篇关于C语言中的array,& array和& array [0]有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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