A
Arin Chaudhuri
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?
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?