[讨论]为什么指向相同地址的指针,取的值不一样?
指针的问题。该程序是在solaries的工作站上运行的。我想要输入一个16进制表示的浮点数,然后转换成16进制的整数。
程序结果是正确的。问题是程序运行结果如下:
put in a single float:c0800000
q is ffbeee00
p is ffbeee00
*p is c0100000
*q is c0800000
***********************************
result is fffffffc
p和q指向同一个地方,为什么输出的内容不一样呢?
原程序
int main(int argc,char **argv)
{
float * p;
// double *pd;
unsigned long int *q;
unsigned long int input;
unsigned long result;
//static char ch,ch1,ch2;
printf("put in a single float:");
scanf("%x",&input); //input is a unsigned long int
getchar(); //receive Enter
q=&input;
printf("q is %p\n",q);
p=(float*)q; //p is a single float pointer
printf("p is %p\n",p);
printf("*p is %x\n",*p);
printf("*q is %x\n",*q);
result=(long int)*p;
printf("***********************************\n");
printf("result is %x\n",result); //result is a single float
}
说明:printf("sizeof(float) is %d",sizeof(float));
printf("sizeof(long int) is %d",sizeof(float));
都是4。
不存在小转大的问题。