gpt4 book ai didi

c - 当我们将值存储在 scanf 中的变量中时,c 编译器如何找到该值的地址?

转载 作者:太空宇宙 更新时间:2023-11-04 06:46:35 24 4
gpt4 key购买 nike

在 scanf("%d",&b);假设我们将 5 存储在 b 变量中,那么 5 的地址将存储在 b 变量中而不是值 5,但是编译器如何找到 5 的地址以及为什么我们不能直接存储值 5?

最佳答案

这不是它的工作原理。通过使用 "%d" 格式说明符,scanf 函数假定参数是指向 int 的指针。所以它获取该指针,使用取消引用运算符,并将值 5 存储在指针指向的位置。

本质上是这样的:

int b;
int *pb = &b; // Point to the variable b
*pb = 5; // Dereference pointer and store value at that location

关于c - 当我们将值存储在 scanf 中的变量中时,c 编译器如何找到该值的地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57256347/

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