本文介绍了这段代码有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 下面的代码是否给出构成双精度的字节有效? / ****** / #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>intmain(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 anunsigned char and extract the values byte by byte as above?2. At the end of the loop p points to a possibly invalid location. Iknow that one can point to one past the end of an array, but here thisdoesn''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 toan object which is not array element behaves like a pointer to an arrayof size one. After the loop, `p'' points to `&w + 1'', i.e. one past whatwould 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. \_/ 我明白你的意思,OP可能也是这样,但它似乎是我b $ b我将有助于这个解释有点明确地说指针 到一个大小为1的字符数组。而不是简单地指向 size one的数组。因为,按照传统的指针算术规则,你的后来的例子表达式是& w + 1。如果基于w的实际类型,将意味着完全不同的东西 (比我想象的要多)。 -leor MartinI understand what you mean, and the OP probably does too, but it seems tome that it would help this explanation a little to explicitly say "pointerto an array of char of size one" rather than simply "pointer to an array ofsize one". Because, as per conventional pointer arithmetic rules, yourlater 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 为了对指针进行算术操作,指向不是数组元素的对象的指针就像指向大小为1的数组的指针。在循环之后,'p''指向'& w + 1'',即如果它是一个数组,那么它将是'w''的最后一个元素。这是允许的。No. For the purpose of arithmentic operations on pointers, a pointer toan object which is not array element behaves like a pointer to an arrayof size one. After the loop, `p'' points to `&w + 1'', i.e. one past whatwould 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 tome that it would help this explanation a little to explicitly say "pointerto an array of char of size one" rather than simply "pointer to an array ofsize one". Because, as per conventional pointer arithmetic rules, yourlater 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 ofthe loop points one byte past the last byte in the double.<<Remove the del for email>> 这篇关于这段代码有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!