在C,如果我投&安培;解引用一个指针,它无关紧要我做的哪一个先? [英] In C, if I cast & dereference a pointer, does it matter which one I do first?

查看:87
本文介绍了在C,如果我投&安培;解引用一个指针,它无关紧要我做的哪一个先?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C,您可以施放既简单数据类型,如 INT 浮动,和指向这些。

In C, you can cast both simple data types like int, float, and pointers to these.

现在我会假设,如果你想从一个指针转换成一种类型到另一种类型的值(例如,从 *浮动 INT ),铸造的顺序和提领无所谓。即对于一个变量浮动* PF ,你有(INT)* PF == *((INT *)PF)。有点像数学可交换...

Now I would have assumed that if you want to convert from a pointer to one type to the value of another type (e.g. from *float to int), the order of casting and dereferencing does not matter. I.e. that for a variable float* pf, you have (int) *pf == *((int*) pf). Sort of like commutativity in mathematics...

然而,这似乎不是这种情况。我写了一个测试程序:

However this does not seem to be the case. I wrote a test program:

#include <stdio.h>
int main(int argc, char *argv[]){
  float f = 3.3;
  float* pf = &f;
  int i1 = (int) (*pf);
  int i2 = *((int*) pf);
  printf("1: %d, 2: %d\n", i1, i2);
  return 0;
}

和我的系统上的输出

1: 3, 2: 1079194419

所以铸造指针似乎从铸造价值的工作方式不同。

So casting the pointer seems to work differently from casting the value.

这是为什么?为什么第二个版本没有做什么,我认为它应该?

Why is that? Why does the second version not do what I think it should?

和是这个平台相关的,还是我莫名其妙地调用未定义的行为?

And is this platform-dependent, or am I somehow invoking undefined behaviour?

推荐答案

以下称获得浮在PF,并将其转换为整数。此处的浇铸是在浮子转换为整数的请求。编译器将生成code浮点值转换为整数值。 (转换浮点值整数是​​一个正常的事情。)

The following says get the float at pf and convert it to an integer. The casting here is a request to convert the float into an integer. The compiler produces code to convert the float value to an integer value. (Converting float values to integers is a "normal" thing to do.)

int i1 = (int) (*pf);

以下称第一个强制编译器认为粉煤指向一个整数(和忽视的事实是PF是一个指向一个float),然后得到整型值(但它不是一个整数值)。这是做一个奇数和危险的事情。在这种情况下,铸造禁用正确转换。编译器在内存(产生垃圾)执行位的简单复制。 (也有可能是内存对齐的问题呢!)

The following says first FORCE the compiler to think pf points to an integer (and ignore the fact that pf is a pointer to a float) and then get the integer value (but it isn't an integer value). This is an odd and dangerous thing to do. The casting in this case DISABLES the proper conversion. The compiler performs a simple copy of the bits at the memory (producing trash). (And there could be memory alignment issues too!)

int i2 = *((int*) pf);

在第二份声明,你不转化​​的指针。你是在告诉编译器什么的记忆点(其中,在这个例子中,就是错误)。

In the second statement, you are not "converting" pointers. You are telling the compiler what the memory points to (which, in this example, is wrong).

这两种说法都在做的非常不同的东西!

These two statements are doing very different things!

请记住,C语言带来的时间使用相同的语法来描述不同的操作。

Keep in mind that c some times uses the same syntax to describe different operations.

=============

=============

注意翻一番是在C默认的浮点类型(数学库通常采用双参数)。

Note that double is the default floating point type in C (the math library typically uses double arguments).

这篇关于在C,如果我投&安培;解引用一个指针,它无关紧要我做的哪一个先?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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