【发布时间】:2016-12-14 13:56:40
【问题描述】:
我无法对指针进行类型转换。我发现了一个垃圾值 Y。 我在我的代码中做错了吗?请帮我找到我想要的输出
#include<stdio.h>
int main(){
float x= 15;
int *y;
y=(int *)&x;
printf("\tValue of X is : %f \n",x);
printf("\tValue of y is : %d",*y);
return 0;
}
输出
【问题讨论】:
-
我的代码有问题吗? -- 是的,你在对编译器撒谎,
float *与int *相同。 -
未定义的行为。
-
A
int和float是两种不同的类型。它们甚至不必是相同的大小。把一个地址当作另一个地址肯定会失败。 -
你能给我正确的代码吗??
-
@ManashKumarMondal 如果您只想将浮点数的值存储在 int 中,那么为什么要使用指针?只需声明一个
int并存储该值。