任何机构都可以解释以下代码的输出 [英] Can any body explain the output of following code

查看:41
本文介绍了任何机构都可以解释以下代码的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

static char buf [10] = {1,2,3,4,5,6,7,8};

printf("地址:%u:Val =%d \\ \\ n,(int *)buf + 1,*((int *)buf + 1));


谢谢


Vija

static char buf[10] = {1,2,3,4,5,6,7,8};
printf("Address: %u : Val = %d \n",(int*)buf+1,*((int*)buf+1));

Thanks

Vija

推荐答案



" srikanth" < GE ******** @ yahoo.com>在消息中写道

news:be ********** @ news.mch.sbs.de ...

"srikanth" <ge********@yahoo.com> wrote in message
news:be**********@news.mch.sbs.de...
static char buf [10] = {1,2,3,4,5,6,7,8};

printf("地址:%u:Val =%d \ n",(int *)buf + 1,*((int *)buf + 1));

感谢

Vija
static char buf[10] = {1,2,3,4,5,6,7,8};
printf("Address: %u : Val = %d \n",(int*)buf+1,*((int*)buf+1));

Thanks

Vija




C ++标准完全没有定义此代码的输出。在我生产的

电脑上它产生了


地址:4390348:Val = 134678021


我没看到那里有什么不寻常的东西。


也许如果你说它在你的电脑上产生了什么,为什么你找到它

很难理解,那么有人能够提供帮助。


也许如果你将134678021转换为十六进制(其0x08070605),你会明白

更好的是发生了什么。


john



The output of this code is completely undefined by the C++ standard. On my
computer it produced

Address: 4390348 : Val = 134678021

I don''t see anything unusual there.

Perhaps if you say what it produces on your computer and why you find it
hard to understand then someone wil be able to help.

Perhaps if you convert 134678021 to hex (its 0x08070605) you will understand
better what is happening.

john


" srikanth" < GE ******** @ yahoo.com>在消息中写道

news:be ********** @ news.mch.sbs.de
"srikanth" <ge********@yahoo.com> wrote in message
news:be**********@news.mch.sbs.de
static char buf [10] = {1, 2,3,4,5,6,7,8};

printf("地址:%u:Val =%d \ n",(int *)buf + 1,* ((int *)buf + 1));

感谢

Vija
static char buf[10] = {1,2,3,4,5,6,7,8};
printf("Address: %u : Val = %d \n",(int*)buf+1,*((int*)buf+1));

Thanks

Vija




您应该:


1.说出你看到的输出,因为这可能取决于系统(在这种情况下是

)。


2.说出让你感到困惑的事情,这样我们就不必猜测

游戏。

表达


(int *)buf + 1

演员操作符的优先级高于+操作符,所以buf是

首次演员到指向int的指针,然后使用指针算法将1添加到指向int的指针,

。对于任何类型T,将1添加到指向T的指针返回

指针值加上sizeof(T)。因此你应该得到buf的地址

加上sizeof(int)。


表达式


* ((int *)buf + 1)


您正在取消引用最后一个表达式。假设sizeof(int)== 4,


(int *)buf + 1


将等于buf [4]的地址和你从解除引用中得到的int

它来自


buff [4] -buf [7],


ie,来自值5,6,7和8.这个int的含义取决于

您的计算机是使用小端还是大端系统。英特尔

处理器使用小端系统,这意味着较低的地址

提供较低的数字。


因此这四个数字读作:


0x08070605

等于


134678021十进制。


如果您的计算机使用大端系统,则数字为

读为


0x05060708


等于


84281096

十进制。

-

John Carson

1.要回复电子邮件地址,请删除donald

2.不要回复电子邮件地址(在此处发布)



You should:

1. Say what output you are seeing, because this may be system dependent (and
is in this case).

2. Say what puzzles you about it so that we don''t have to engage in guessing
games.
In the expression

(int*)buf+1

the cast operator has a higher precedence than the + operator, so buf is
first cast to a pointer-to-int and then 1 is added to this pointer to int,
using pointer arithmetic. For any type T, adding 1 to a pointer-to-T returns
the pointer value plus sizeof(T). Thus you should get the address of buf
plus sizeof(int).

In the expression

*((int*)buf+1)

you are dereferencing the last expression. Assuming sizeof(int)==4,

(int*)buf+1

will equal the address of buf[4] and the int that you get from dereferencing
it is formed from

buff[4]-buf[7],

i.e., from the values 5,6,7 and 8. What this int looks like depends on
whether your computer uses a little endian or big endian system. Intel
processors use the little endian system, which means that lower addresses
supply the less significant digits.

Thus the four digits are read as:

0x08070605

which equals

134678021 decimal.

If you have a computer that uses the big endian system, then the digits are
read as

0x05060708

which equals

84281096

decimal.
--
John Carson
1. To reply to email address, remove donald
2. Don''t reply to email address (post here instead)


谢谢John,

现在我理解得很好

你知道以下编译器吗?大端格式

问候

Vijay
Thanks John,
Now I understood it well
Do you know any compiler which follows Big endian format
Regards
Vijay


这篇关于任何机构都可以解释以下代码的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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