C语言 指针/地址类型转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2733960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:18:52  来源:igfitidea点击:

Pointer/address type casting

c

提问by AnT

I have the following variables:

我有以下变量:

char *p;
int l=65;

Why do the following casts fail?

为什么以下强制转换失败?

(int *)p=&l;

and:

和:

p=&((char) l);

回答by AnT

The result of type conversion is always an rvalue. Rvalue cannot be assigned to, which is why your first expression doesn't compile. Rvalue cannot be taken address of, which is why your second expression doesn't compile.

类型转换的结果始终是rvalue。无法将右值分配给,这就是您的第一个表达式无法编译的原因。右值不能被取地址,这就是为什么你的第二个表达式不能编译。

In order to perform the correct type conversion, you have to to it as follows

为了执行正确的类型转换,您必须按如下方式进行

p = (char *) &l;

This is the proper way to do what you tried to do in your second expression. It converts int *pointer to char *type.

这是执行您在第二个表达式中尝试执行的操作的正确方法。它将int *指针转换为char *类型。

Your first expression is beyond repair. You can do

你的第一个表情是无法修复的。你可以做

*(int **) &p = &l;  

but what it does in the end is not really a conversion, but rather reinterpretationof the memory occupied by char *pointer as int *pointer. It is an ugly illegal hack that most of the time has very little practical value.

但它最终所做的并不是真正的转换,而是char *指针占用的内存重新解释int *指针。这是一个丑陋的非法黑客,大多数时候几乎没有实用价值。

回答by James McNellis

The correct way to do this would be:

正确的方法是:

int I = 65;
char* p = (char*)&I;

&Igives you an int*that points to I; you then cast this to a char*and assign it to p.

&I给你一个int*指向I; 然后char*将其转换为 a并将其分配给p.

Note that you shouldn't ordinarily cast between pointers of unrelated types. A char*can be used to access any object, though, so in this particular case it is safe.

请注意,您通常不应在不相关类型的指针之间进行转换。但是,Achar*可用于访问任何对象,因此在这种特殊情况下它是安全的。

回答by Michael Aaron Safyan

(int *)p=&l;

(int *)p=&l;

The line above doesn't work, because as soon as you cast pto (int*), the result is an anonymous temporary object, which is an rvalueand not an lvalue; consquently, the result cannot receive the assignment, and even if the language did allow it, you'd be assigning to a temporary casted copy of p, not to the original p.

上面的行不起作用,因为一旦你转换p(int*),结果就是一个匿名临时对象,它是一个rvalue而不是一个lvalue;因此,结果无法接受分配,即使语言确实允许,您也会分配给 的临时铸造副本p,而不是原始p.

p=&((char) l);

p=&((char) l);

The line above does not work for a similar reason; the result of (char) lis a temporary object that is a copy of lcasted to the type char. Consequently, because it is temporary, you cannot take its address.

由于类似的原因,上面的行不起作用;的结果(char) l是一个临时对象,它是l强制转换为 char 类型的副本。因此,由于它是临时的,您无法获取其地址。

Insead, you can use:

Insead,您可以使用:

p = (char*) &l

回答by Michael Burr

The problem is that when you're performing the casts (aside from whether or not the kind of casting you're doing is a good idea or not) is that the cast expression results in an rvalue.

问题在于,当您执行转换时(除了您正在执行的转换类型是否是一个好主意)是转换表达式会产生一个右值。

rvalues cannot be assigned to or have their addreses taken.

不能将右值分配给或使用它们的地址。

回答by M. Williams

In plain C, which is not that strict to type conversions, this code would compile and actually work. On a C++ compiler it would actually require explicit casts as already mentioned (see other answers).

在对类型转换不那么严格的普通 C 中,这段代码可以编译并实际工作。在 C++ 编译器上,它实际上需要如前所述的显式强制转换(参见其他答案)。