gpt4 book ai didi

c - 为什么整数隐式转换为指针?

转载 作者:太空宇宙 更新时间:2023-11-04 07:53:48 25 4
gpt4 key购买 nike

我看到一个question使用此语句:printf(scanf("%d",&a));

当然它会生成警告 passing argument 1 of ‘printf’ makes pointer from integer without a cast.

我很清楚这种说法有什么问题。这不是问题。

但这让我想知道。我完全理解为什么允许在 float 和整数之间进行隐式转换,即使它容易出错,但我不明白为什么允许从整数到指针。您什么时候会将整数转换为指针?

最佳答案

I perfectly understand why implicit casts between floats and integers is allowed even though it is error prone, but I cannot see why it is allowed from integer to pointer. When would you ever what to do this?

考虑一个需要访问特定地址的软件。假设引导加载程序想要获取 VGA 文本模式缓冲区的句柄,该缓冲区位于物理地址 0xB8000

uint16_t * terminal_buffer = (uint16_t*)0xB8000;

此行将整数 0xB8000 转换为指针 转换,这是完全合法的。如果您省略 转换,则有可能您没有做您认为正在做的事情(就像您的printf( scanf( .. . ) ) 示例),这就是编译器发出警告的原因。

关于c - 为什么整数隐式转换为指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51919174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com