这段代码有效吗? [英] Is this code valid

查看:78
本文介绍了这段代码有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



下面的代码是否给出构成双精度的字节有效?

/ ****** /

#include< stdio.h>


int

main(无效)

{

double w = 0.1;

unsigned char * p;

unsigned char c;

size_t i;

p =(unsigned char *)& w;

for(i = 0; i< sizeof w; i ++,p ++)

{

c = * p;

printf("%X",(unsigned)c);

}

getchar();

返回0;

}

/ **** /


我的问题是:

1.是否允许将指向double的指针强制转换为指向

unsigned char的指针并按字节逐字节提取值?


2.在循环结束时,p指向可能无效的位置。我知道一个人可以指向一个超过数组末尾的一个,但是这里这个

并不成立。这会导致未定义的行为吗?

Hi,
Is the code below that gives the bytes that make up a double valid?
/******/
#include <stdio.h>

int
main(void)
{
double w=0.1;
unsigned char *p;
unsigned char c;
size_t i;
p=(unsigned char *)&w;
for(i=0;i < sizeof w;i++,p++)
{
c=*p;
printf("%X ",(unsigned) c);
}
getchar();
return 0;
}
/****/

My questions are:
1. Is it permissible to cast a pointer to a double to a pointer to an
unsigned char and extract the values byte by byte as above?

2. At the end of the loop p points to a possibly invalid location. I
know that one can point to one past the end of an array, but here this
doesn''t hold. Does this lead to undefined behavior?

推荐答案

ar ***************** @ yahoo.com (Arin Chaudhuri)写道:
ar*****************@yahoo.com (Arin Chaudhuri) writes:

下面的代码是否给出构成双精度的字节有效?
/ ****** /
#include< stdio.h>

int
main(无效)
{
双w = 0.1;
unsigned char * p;
unsigned char c;
size_t i;
p =(unsigned char *)& w;
for(i = 0; i< sizeof w; i ++,p ++)
{
c = * p;
printf("%X",(unsigned)c);
}
getchar();
返回0;
}
/ **** /

我的问题是:
1.是否允许将指向double的指针强制转换为指向
unsigned char的指针并提取值byte按字节如上?


是的。这被称为访问表示和表示。一个对象。

2.在循环结束时,p指向可能无效的位置。我知道一个人可以指向一个超过数组结尾的一个,但是这里没有。这会导致未定义的行为吗?
Hi,
Is the code below that gives the bytes that make up a double valid?
/******/
#include <stdio.h>

int
main(void)
{
double w=0.1;
unsigned char *p;
unsigned char c;
size_t i;
p=(unsigned char *)&w;
for(i=0;i < sizeof w;i++,p++)
{
c=*p;
printf("%X ",(unsigned) c);
}
getchar();
return 0;
}
/****/

My questions are:
1. Is it permissible to cast a pointer to a double to a pointer to an
unsigned char and extract the values byte by byte as above?
Yes. This is know as accessing the "representation" of an object.
2. At the end of the loop p points to a possibly invalid location. I
know that one can point to one past the end of an array, but here this
doesn''t hold. Does this lead to undefined behavior?




编号为了对指针进行算术操作,指向

的对象不是数组元素的行为类似于指向数组的指针

的大小为1。在循环之后,'p''指向'& w + 1'',即,如果它是一个数组,那么

将是'w''的最后一个元素。那是允许的。


马丁

-

, - 。 Martin Dickopp,德国德累斯顿,=, - _-。 =。

/, - ) http://www.zero -based.org/ ((_ /)oo(\_))

\` - ''` - ''(。)` - ''

` - 。 Debian,GNU操作系统的一种变体。 \_ /



No. For the purpose of arithmentic operations on pointers, a pointer to
an object which is not array element behaves like a pointer to an array
of size one. After the loop, `p'' points to `&w + 1'', i.e. one past what
would be the last element of `w'' if it were an array. That''s allowed.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-'' `-''(. .)`-''
`-. Debian, a variant of the GNU operating system. \_/


2004年5月28日星期五01:09:36 +0200,Martin Dickopp

< ex **** ************@zero-based.org>写道:
On Fri, 28 May 2004 01:09:36 +0200, Martin Dickopp
<ex****************@zero-based.org> wrote:
2.在循环结束时,p指向可能无效的位置。我知道一个人可以指向一个超过数组结尾的一个,但是这里没有。这是否会导致未定义的行为?
否为了对指针进行算术操作,指向不是数组元素的对象的指针就像指向大小为1的数组的指针。在循环之后,'p''指向'& w + 1'',即如果它是一个数组,那么它将是'w''的最后一个元素。这是允许的。
2. At the end of the loop p points to a possibly invalid location. I
know that one can point to one past the end of an array, but here this
doesn''t hold. Does this lead to undefined behavior?
No. For the purpose of arithmentic operations on pointers, a pointer to
an object which is not array element behaves like a pointer to an array
of size one. After the loop, `p'' points to `&w + 1'', i.e. one past what
would be the last element of `w'' if it were an array. That''s allowed.




我明白你的意思,OP可能也是这样,但它似乎是我b $ b我将有助于这个解释有点明确地说指针

到一个大小为1的字符数组。而不是简单地指向

size one的数组。因为,按照传统的指针算术规则,你的后来的例子表达式是& w + 1。如果基于w的实际类型,将意味着完全不同的东西

(比我想象的要多)。

-leor

Martin



I understand what you mean, and the OP probably does too, but it seems to
me that it would help this explanation a little to explicitly say "pointer
to an array of char of size one" rather than simply "pointer to an array of
size one". Because, as per conventional pointer arithmetic rules, your
later example expression "&w + 1" would mean something totally different
(than what I think you intended) if based on w''s actual type.
-leor

Martin




-

Leor Zolman --- BD软件--- www.bdsoft.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt.html


2004年5月27日星期四19:53:23 -0400,Leor Zolman< ; le ** @ bdsoft.com>

写道:
On Thu, 27 May 2004 19:53:23 -0400, Leor Zolman <le**@bdsoft.com>
wrote:
On Fri,2004年5月28日01:09:36 +0200,Martin Dickopp <前**************** @ zero-based.org>写道:
On Fri, 28 May 2004 01:09:36 +0200, Martin Dickopp
<ex****************@zero-based.org> wrote:
2.在循环结束时,p指向可能无效的位置。我知道一个人可以指向一个超过数组结尾的一个,但是这里没有。这是否会导致未定义的行为?
2. At the end of the loop p points to a possibly invalid location. I
know that one can point to one past the end of an array, but here this
doesn''t hold. Does this lead to undefined behavior?



为了对指针进行算术操作,指向不是数组元素的对象的指针就像指向大小为1的数组的指针。在循环之后,'p''指向'& w + 1'',即如果它是一个数组,那么它将是'w''的最后一个元素。这是允许的。



No. For the purpose of arithmentic operations on pointers, a pointer to
an object which is not array element behaves like a pointer to an array
of size one. After the loop, `p'' points to `&w + 1'', i.e. one past what
would be the last element of `w'' if it were an array. That''s allowed.



我理解你的意思,而且OP可能也是这样,但似乎这对我的解释有点明确对一个大小为1的char数组说指针。而不是简单地指向一个大小为一的数组。因为,按照传统的指针算术规则,你的
后面的例子表达式& w + 1和如果基于w的实际类型,那将意味着完全不同的东西(比我想象的那样)。



I understand what you mean, and the OP probably does too, but it seems to
me that it would help this explanation a little to explicitly say "pointer
to an array of char of size one" rather than simply "pointer to an array of
size one". Because, as per conventional pointer arithmetic rules, your
later example expression "&w + 1" would mean something totally different
(than what I think you intended) if based on w''s actual type.




但是在这种情况下,p点到一个双精度数组,在
结尾处,循环指向双精度数字的最后一个字节后的一个字节。

<<删除电子邮件的del> >



But in this case, p points to an array of 1 double and at the end of
the loop points one byte past the last byte in the double.
<<Remove the del for email>>


这篇关于这段代码有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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