何时强制类型转换指针? [英] When is typecasting a pointer allowed?

查看:70
本文介绍了何时强制类型转换指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为考试而学习,下面给出的示例问题之一:



以下表达式中的哪些被分配给new_val(表达式将替换 HERE )将打印 7 ?选择所有适用的内容:

  float m = 7.0 
int * a =(int *)& m;

int new_val =此处;
printf(%d\n,new_val);

(a)*(int *)& m



(b)* a



(c)(int)m



(d)






答案仅是(c)



我已经在自己的计算机上运行了该程序,我发现答案是正确的。但是,我想知道有人怎么能找到这个答案?一般来说,我在理解有关强制转换指针的规则时遇到了麻烦。我在StackOverflow上读了其他几篇文章,对我的帮助不太大。



我想知道是否有人可以帮助阐明我的工作方式与此类似的问题。

解决方案

因为 a =(int *)& m ,选项(a)和(b)相同。在这两种情况下,您都使用地址 m ,将其强制转换为 int * ,然后取消引用。 / p>

(a)和(b)不起作用的原因是& m 是指向 float float * ),它不是指向 int int * )。



考虑两个指针,一个是指向 Window ,另一个是指向操纵杆的指针。很明显,您不能将它们混淆,对吗?您无法将指向 Window 的指针传递给期望指向 Joystick 指针的函数。您可以投射指针,但这不会使 Window 成为 Joystick



所以指向 float 的指针和指向 int 的指针是不兼容的,如果将指针转换为 float 的指针为 int 的指针,然后尝试取消引用它,将会得到垃圾。



但是(c)起作用的原因是,这里您要获取实际的 float m 并要求编译器将该值转换为 int 值。编译器知道如何执行此操作并生成正确的代码。



您可能想知道,如果编译器知道如何转换 float 转换为 int ,为什么它不能将指向 float 的指针转换为指向 int ?实际上确实如此;这就是演员的目的。但是它会转换 pointer 而不是指针所指向的


I'm studying for an exam, and one of the sample questions I've been given is the following:

"Which of the following expressions when assigned to new_val (the expression will replace HERE) will print 7?" Choose all that apply:

float m = 7.0
int *a = (int *) &m;

int new_val = HERE;
printf("%d\n", new_val);

(a) *(int *) &m

(b) *a

(c) (int) m

(d) None of the above.


The answer is (c) only.

I've run the program on my own machine, and what I found agrees with the answer. However, I was wondering how someone can reason to get to this answer? More generally, I'm having trouble understanding the rules regarding casting pointers. I've read a couple of other posts on StackOverflow, which weren't too helpful to me.

I was wondering if someone could please help clarify how I can work through questions similar to this one.

解决方案

Because a = (int *)&m, options (a) and (b) are identical. In both cases you're taking the address of m, casting it to int *, and then dereferencing it.

The reason that (a) and (b) don't work is that &m is a pointer to float (a float *) which is not a pointer to int (int *).

Consider two pointers, one is a pointer to a Window, and another is a pointer to a Joystick. It's pretty clear that you can't mix those up, right? You couldn't pass a pointer-to-Window to a function that expects a pointer-to-Joystick. You could cast the pointer, but that doesn't make the Window into a Joystick.

So pointer to float and pointer to int are just incompatible, and if you cast a pointer to float to a pointer to int and try to dereference it, you'll get garbage.

But (c) works because here you're taking the actual float value m and asking the compiler to convert that value into an int value. The compiler knows how to do this and generates the correct code.

You're probably wondering, if the compiler knows how to convert a float into an int, why can't it convert the pointer to float into a pointer to int? In fact it does; that's the purpose of the cast. But it converts the pointer not the thing the pointer is pointing to.

这篇关于何时强制类型转换指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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