简体   繁体   English

int *p=10 和 int *p = (int *)10 有什么区别?

[英]Whats the difference between int *p=10 and int *p = (int *)10?

do both statements mean the same thing that p is pointing at add loc 10?这两个语句的含义是否与 p 指向的 add loc 10 相同? On compilation, the first initialization gives some warning.在编译时,第一次初始化会给出一些警告。 What's the meaning of that?那是什么意思?

#include <stdio.h>
int main()
{
    int *p = 10;
    int *q = (int *)10;

    return 0;
}

output: output:

warning: initialization of ‘int *’ from ‘int’ makes pointer from integer without a cast [- Wint-conversion]

Both cases convert the integer 10 to a pointer type which is used to initialize an int * .这两种情况都将 integer 10 转换为用于初始化int *的指针类型。 The cast in the second case makes it explicit that this behavior is intentional.第二种情况的演员表明确表明这种行为是故意的。

While converting from an integer to pointer is allowed, the assignment operator (and by extension, initialization) does not specifically allow this conversion, so a cast it required to be conforming.虽然允许从 integer 转换为指针,但赋值运算符(以及通过扩展初始化)并没有明确允许这种转换,因此需要符合要求的强制转换。 Many compilers however will still allow this and simply issue a warning (as your apparently does).然而,许多编译器仍然允许这样做并简单地发出警告(就像你显然那样)。

Note however that actually attempting to use a pointer that is assigned a specific numeric value will most likely cause a crash unless you're on a embedded system that supports reading or writing specific memory addresses.但是请注意,实际尝试使用分配了特定数值的指针很可能会导致崩溃,除非您使用的是支持读取或写入特定 memory 地址的嵌入式系统。

int *p = 10; is incorrect (constraint violation), and the compiler must produce a diagnostic message.不正确(违反约束),编译器必须生成诊断消息。 The compiler could reject the program, and there is no behaviour defined if it doesn't.编译器可以拒绝该程序,如果没有,则没有定义行为。 The rule is that the initializer for a pointer must be a compatible pointer value or a null pointer constant.规则是指针的初始化程序必须是兼容的指针值或 null 指针常量。

int *q = (int *)10; means to convert the integer 10 to a pointer.表示将 integer 10转换为指针。 The result is implementation-defined and it could be a trap representation, meaning that the initialization causes undefined behaviour if execution reaches this line.结果是实现定义的,它可能是一个陷阱表示,这意味着如果执行到达这一行,初始化会导致未定义的行为。

int and pointer to an integer int* are different types. int和指向 integer int*的指针是不同的类型。 The 10 on the first line is an int that you are trying to assign to a pointer to int type.第一行的10是一个int ,您试图将其分配给指向 int 类型的指针。 Hence the warning.因此发出警告。 (on X86 both share the same size, but consider that mostly coincidence at this point). (在 X86 上,两者的大小相同,但此时考虑到这主要是巧合)。

By casting the int to a pointer, like you do on the second line, you are telling the compiler "Hey, I know these are different types but I know what I'm doing, so go ahead and just treat the value 10 like a pointer because I really do want to point at the memory with an address of 10".通过将 int 转换为指针,就像您在第二行所做的那样,您是在告诉编译器“嘿,我知道这些是不同的类型,但我知道我在做什么,所以 go 提前并将值 10 视为指针,因为我真的想指向地址为 10" 的 memory。 (in almost every case the memory address of 10 is not going to be usable by you) (几乎在所有情况下,您都无法使用 10 的 memory 地址)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM