数组名称及其地址。他们可以是一样的吗? [英] array name and its address. Can they be same

查看:64
本文介绍了数组名称及其地址。他们可以是一样的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我对下面这段代码有疑问:


int a [10];

printf (a =%p \ n,a);

printf("& a =%p \ n",& a);

这些printf语句为a和& a打印相同的值。

我在VC ++版本6和Redhat Enterprise Linux 4中尝试过。

这些内存位置应该不同吗?

这种行为的原因是什么


谢谢
su ************** @ yahoo.com

Hello
I have a doubt in the following piece of code:

int a[10];
printf("a=%p\n", a);
printf("&a=%p\n", &a);

these printf statements print the same value for both ''a'' and ''&a".
I tried in VC++version 6 and also Redhat Enterprise Linux 4.
Should'' t these memory locations be different?
What is the reason for this behaviour

Thanks
su**************@yahoo.com

推荐答案

subramanian说:
subramanian said:

你好

我对下面这段代码有疑问:


int a [10];

printf(" a =%p \\ \\ n",a);

printf("& a =%p \ n",& a);
Hello
I have a doubt in the following piece of code:

int a[10];
printf("a=%p\n", a);
printf("&a=%p\n", &a);



如果你提供一个非void *类型的指针给printf作为匹配

的%p说明符,那么该程序未定义。

If you supply a pointer of a type other than void * to printf as a match for
its %p specifier, the behaviour of the program is undefined.


这些printf语句为'a''和''& a打印相同的值。
these printf statements print the same value for both ''a'' and ''&a".



a是一个10 int的数组。当在值上下文中使用时,表达式将

衰减为指向该数组中第一个元素的指针。也就是说,它等于& a [0]是



& a是指向10 int数组的指针。它有一个不同的类型。

顺便提一句,a是示例代码中令人震惊的名称选择。

a is an array of 10 int. When used in a value context, the expression decays
into a pointer to the first element in that array. That is, it is
equivalent to &a[0].

&a is a pointer to an array of 10 int. It has a different type to a.
Incidentally, a is an appalling choice of name in example code.


我在VC +中尝试过+版本6以及Redhat Enterprise Linux 4.

这些内存位置是否应该不同?
I tried in VC++version 6 and also Redhat Enterprise Linux 4.
Should'' t these memory locations be different?



为什么?

Why?


这种行为的原因是什么
What is the reason for this behaviour



行为未定义。尽管如此,以下程序可能会产生相同的结果:


#include< stdio.h>


int main(无效)

{

int foo [10];

printf(" foo =%p \ n" ;,(void *)foo);

printf("& foo =%p \ n",(void *)& foo);

return 0;

}


这个程序可能为foo打印相同指针值的原因

和& foo是数组是一个聚合对象,其中没有

填充,因此该数组包含(在这种情况下)十个成员

完全适合它的对象: br />

+ -------- + -------- + -------- + -------- + - ------ + -------- + ...

| foo [0] | foo [1] | foo [2] | foo [3] | foo [4] | foo [5] | ...

+ -------- + -------- + -------- + -------- + - ------ + -------- + --...

| foo ...

+ -------- + -------- + -------- + -------- + - ------- + -------- + --...


正如你所看到的,foo [0]和foo都有一个共同的开头点。所以他们

都在同一个地址。不过,它们仍然是不同类型的b $ b。


-

Richard Heathfield

" ; Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:正常服务将尽快恢复。请不要

调整您的电子邮件客户端。

The behaviour is undefined. Nevertheless, the following program is highly
likely to produce equivalent results:

#include <stdio.h>

int main(void)
{
int foo[10];
printf("foo=%p\n", (void *)foo);
printf("&foo=%p\n", (void *)&foo);
return 0;
}

The reason this program is likely to print the same pointer value for foo
and &foo is that an array is an aggregate object in which there is no
padding, and therefore the array comprises (in this case) ten member
objects that fit into it exactly:

+--------+--------+--------+--------+--------+--------+ ...
| foo[0] | foo[1] | foo[2] | foo[3] | foo[4] | foo[5] | ...
+--------+--------+--------+--------+--------+--------+--...
| foo ...
+--------+--------+--------+--------+--------+--------+--...

As you can see, both foo[0] and foo share a common beginning point. So they
are both at the same address. Nevertheless, they are still of different
types.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.


Richard Heathfield写道:
Richard Heathfield wrote:

>

subramanian说:
>
subramanian said:

你好

我对下面这段代码有疑问:


int a [10];

printf(" a =%p \ n",a);

printf("& a =%p \ n",& a);
Hello
I have a doubt in the following piece of code:

int a[10];
printf("a=%p\n", a);
printf("&a=%p\n", &a);



如果你提供一个非void *类型的指针到printf作为匹配

的%p说明符,那么该程序未定义。


If you supply a pointer of a type other than void * to printf as a match for
its %p specifier, the behaviour of the program is undefined.


这些printf语句为'a''和''& a打印相同的值。
these printf statements print the same value for both ''a'' and ''&a".



a是一个10 int的数组。当在值上下文中使用时,表达式将

衰减为指向该数组中第一个元素的指针。也就是说,它等于& a [0]是



& a是指向10 int数组的指针。它有一个不同的类型。

顺便提一句,a是示例代码中令人震惊的名称选择。


a is an array of 10 int. When used in a value context, the expression decays
into a pointer to the first element in that array. That is, it is
equivalent to &a[0].

&a is a pointer to an array of 10 int. It has a different type to a.
Incidentally, a is an appalling choice of name in example code.


我在VC +中尝试过+版本6以及Redhat Enterprise Linux 4.

这些内存位置是否应该不同?
I tried in VC++version 6 and also Redhat Enterprise Linux 4.
Should'' t these memory locations be different?



为什么?


Why?


这种行为的原因是什么
What is the reason for this behaviour



行为未定义。

尽管如此,以下程序非常可能会产生相同的结果:


#include< stdio.h>


int main(无效)

{

int foo [10] ;

printf(" foo =%p \ n",(void *)foo);

printf("& foo =%p \ n" ;,(void *)& foo);

返回0;

}


此程序可能的原因

为foo打印相同的指针值

和& foo是一个数组是一个聚合对象,其中没有

填充,因此,数组包含(在这种情况下)十个成员

完全适合它的对象:


+ -------- + - ------- + -------- + -------- + -------- + -------- + ...

| foo [0] | foo [1] | foo [2] | foo [3] | foo [4] | foo [5] | ...

+ -------- + -------- + -------- + -------- + - ------ + -------- + --...

| foo ...

+ -------- + -------- + -------- + -------- + - ------- + -------- + --...


正如你所看到的,foo [0]和foo都有一个共同的开头点。

所以他们都在同一个地址。

尽管如此,他们仍然是不同的类型。


The behaviour is undefined.
Nevertheless, the following program is highly
likely to produce equivalent results:

#include <stdio.h>

int main(void)
{
int foo[10];
printf("foo=%p\n", (void *)foo);
printf("&foo=%p\n", (void *)&foo);
return 0;
}

The reason this program is likely
to print the same pointer value for foo
and &foo is that an array is an aggregate object in which there is no
padding, and therefore the array comprises (in this case) ten member
objects that fit into it exactly:

+--------+--------+--------+--------+--------+--------+ ...
| foo[0] | foo[1] | foo[2] | foo[3] | foo[4] | foo[5] | ...
+--------+--------+--------+--------+--------+--------+--...
| foo ...
+--------+--------+--------+--------+--------+--------+--...

As you can see, both foo[0] and foo share a common beginning point.
So they are both at the same address.
Nevertheless, they are still of different types.



((char *)& foo)等于((char *)foo),和

(char *)和(void *)有相同的表示,

所以我不认为不同的类型,有所作为。


-

pete

((char *)&foo) is equal to ((char *)foo), and
(char *) and (void *) have the same representation,
so I don''t think that the different types, makes a difference.

--
pete


皮特说:
pete said:

Richard Heathfield写道:
Richard Heathfield wrote:

>>
>>



< snip>

<snip>


>>
如您所见,foo [0]和foo共享一个共同的起点。
所以他们都在同一个地址。
尽管如此,他们仍然是不同的类型。
>>
As you can see, both foo[0] and foo share a common beginning point.
So they are both at the same address.
Nevertheless, they are still of different types.



((char *)& foo)等于((char *)foo),和

(char *)和(void *)有相同的表示,

所以我不认为不同的类型,有所作为。


((char *)&foo) is equal to ((char *)foo), and
(char *) and (void *) have the same representation,
so I don''t think that the different types, makes a difference.



不是他们的地址,没有。我说得那么清楚,不是吗?我是什么

试图确保OP没有消失,认为foo和& foo是可互换的,可互换的或具有相同语义的.b / b。 >

-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:正常服务将尽快恢复。请不要

调整您的电子邮件客户端。

Not to their address, no. I made that clear enough, did I not? What I was
trying to ensure was that the OP did not go away thinking foo and &foo are
interchangeable, fungible, or possessed of identical semantics.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.


这篇关于数组名称及其地址。他们可以是一样的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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