输出printf(a,* a,** a),其中a的类型为int [] [] [] [英] Output of the printf(a,*a,**a) where a is of type int [][][]

查看:67
本文介绍了输出printf(a,* a,** a),其中a的类型为int [] [] []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdio.h>


int main()

{

int a [3] [3 ] [3];

printf("%d%d%d%d",a,* a,** a,& a);

}


我尝试了上面的代码并获得了相同的价值,a,* a,** a和& a。

任何人都可以告诉我这种行为背后的原因是什么?

#include<stdio.h>

int main()
{
int a[3][3][3];
printf("%d %d %d %d",a,*a,**a,&a);
}

I tried the above code and got the same value for a, *a , **a and &a.
Can anyone please tell me the reason behind such a behavior?

推荐答案

文章< 73 ****************** **************** @ l32g2000hse。 googlegroups.com>,

Nikhil Bokare< nb ***** @ gmail.comwrote:
In article <73**********************************@l32g2000hse. googlegroups.com>,
Nikhil Bokare <nb*****@gmail.comwrote:

> #include< stdio .H>
>#include<stdio.h>


> int main()
{
int a [3] [3] [3];
printf("%d%d%d%d",a,* a,** a,& a);
}
>int main()
{
int a[3][3][3];
printf("%d %d %d %d",a,*a,**a,&a);
}


>我尝试了上面的代码并为a,* a,** a和& a获得了相同的值。
任何人都可以告诉我这种行为背后的原因吗?
>I tried the above code and got the same value for a, *a , **a and &a.
Can anyone please tell me the reason behind such a behavior?



你不应该打印出%d格式的指针。一个

指针可能(或可能不会)占用比int更多的存储空间,

所以如果你使用%d格式你可能只打印出部分

指针的地址。指针

比int长的情况并不少见;指向-happen-

的指针更常见的是长度相同,但不能保证。

您可以使用%p格式写出来void指向void

(所以一定要把指针转换成参数列表中的(void *),

作为指向不同类型的指针可能有不同的大小。)


另外,你已经指定你的main返回int但是你没有从main()返回任何值。

。这将在C99​​中工作

但在C90中有未定义的行为。

-

我将推测[...]应用程序[...]实际上可以通过双核[...]看到大多数用户的性能提升......因为它b / b
正在运行广告软件和间谍软件[ ......否则会降低用户今天使用的单CPU的速度。 - Herb Sutter

You should not be printing out pointers with a %d format. A
pointer might (or might not) occupy more storage than an int,
so if you use a %d format you might be printing out only part
of the address of the pointer. It is not uncommon for pointers
to be longer than int; it is more common for pointers to -happen-
to be the same size as long, but that is not guaranteed.
You can use a %p format to write out a pointer to void
(so be sure to cast the pointer to (void *) in the argument list,
as pointers to different types may have different sizes.)

Also, you have specified that your main returns int and yet
you have not returned any value from main(). That will work in C99
but has undefined behaviour in C90.
--
"I will speculate that [...] applications [...] could actually see a
performance boost for most users by going dual-core [...] because it
is running the adware and spyware that [...] are otherwise slowing
down the single CPU that user has today" -- Herb Sutter


Nikhil Bokare说:
Nikhil Bokare said:

#include< stdio.h>


int main()

{

int a [3] [3] [3];

printf (%d%d%d%d,a,* a,** a,& a);

}


我试过了上面的代码和a,* a,** a和& a得到相同的值。
#include<stdio.h>

int main()
{
int a[3][3][3];
printf("%d %d %d %d",a,*a,**a,&a);
}

I tried the above code and got the same value for a, *a , **a and &a.



他们不能拥有相同的价值,因为他们没有相同的类型。 a

的类型为int [3] [3] [3],* a的类型为int [3] [3],** a的类型为int [3],& a

有类型(int *)[3] [3] [3]。这些都不是int类型,所以%d是

不适合作为格式说明符(事实上,如果你这样做,那么这个行为是未定义的)。由于printf缺少

上述类型的格式说明符,因此需要进行转换,并且最明显的方式

这样做是为了转换为void *,其中当然丢弃类型

信息,所以当你纠正你的程序使用casts-to-void *和%p

而不是%d时,你不一定期望看到不同的价值观。

They can''t have the same value, because they don''t have the same type. a
has type int[3][3][3], *a has type int[3][3], **a has type int[3], and &a
has type (int *)[3][3][3]. None of these is type int, so %d is
inappropriate as a format specifier (and indeed the behaviour if you do
this is undefined). Since printf lacks format specifiers for the
above-mentioned types, a conversion is required, and the most obvious way
to do this is to cast to void *, which of course discards the type
information, so when you correct your program to use casts-to-void* and %p
instead of %d, you shouldn''t necessarily expect to see different values.


有谁能告诉我这种行为背后的原因?
Can anyone please tell me the reason behind such a behavior?



Mu。


-

Richard Heathfield< http:// www。 cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Mu.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


Nikhil Bokare< nb ***** @ gmail.comwrites:
Nikhil Bokare <nb*****@gmail.comwrites:

#include< stdio.h>


int main()

{

int a [3] [3] [ 3];

printf("%d%d%d%d",a,* a,** a,& a);

}


我尝试了上面的代码并获得了相同的价值,a,* a,** a和& a。

有谁能告诉我背后的原因这样的行为?
#include<stdio.h>

int main()
{
int a[3][3][3];
printf("%d %d %d %d",a,*a,**a,&a);
}

I tried the above code and got the same value for a, *a , **a and &a.
Can anyone please tell me the reason behind such a behavior?



你很幸运(或不幸)该计划完全有效。 %d

格式需要int类型的参数。如果你给它任何东西

else,你调用未定义的行为。你可能会在实现上得到有意义的结果,但是没有保证。


如果你想打印一个指针值,请使用" %p"并将

参数转换为void *。


您还应使用换行符(\ n)终止输出并返回
main()的
a值; return 0;通常是正确的。


到达你真正想要的东西,你应该阅读comp.lang.c的第6节

常见问题解答,< http://www.c-faq.com/>。如果您在此之后仍然感到困惑,请随时回复更具体的

问题。


-

Keith Thompson(The_Other_Keith)< ks *** @ mib.org>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

You''re lucky (or unlucky) that the program worked at all. The "%d"
format requires an argument of type int. If you give it anything
else, you invoke undefined behavior. You''re probably getting
meaningful results on your implementation, but there are no guarantees.

If you want to print a pointer value, use "%p" and convert the
argument to void*.

You should also terminate your output with a newline ("\n") and return
a value from main(); "return 0;" is usually the right thing.

Getting to what you''re really asking about, you should read section 6
of the comp.lang.c FAQ, <http://www.c-faq.com/>. If you''re still
confused after that, feel free to come back with more specific
questions.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


这篇关于输出printf(a,* a,** a),其中a的类型为int [] [] []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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