gpt4 book ai didi

c - 指针和地址?

转载 作者:行者123 更新时间:2023-11-30 21:47:50 26 4
gpt4 key购买 nike

我输入了以下内容:

#include <stdio.h>

int main ()
{
int *ptr, x, y;
ptr = &x;
y = x;

printf("This is the value on location of x: %p\n", *ptr);
printf("This is the address of x: %p\n", &x);
printf("The value of x is *ptr is: %i %x\n", *ptr);
printf("This is the address of ptr: %p\n", &ptr);
printf("The value of y is: %i\n", y);
printf("The address of y is %p\n", &y);

return 0;
}

控制台显示以下内容:

This is the address of x: 000004F
This is the address of x: 0028FF18
The value of x is *ptr is: 79 28ff94
This is the address of ptr: 0028FF1C
The value of y is 0028FF14

我可以理解所有 printf 结果,但除了第三行的第二个值 28ff94?我知道 *ptr = x 的第一个值 %d79%x000004F。我明确没有指定第二个值,为空。但现在我想知道十六进制值 28ff94 来自哪里?

最佳答案

访问未初始化的变量会导致未定义的行为。程序可能只是简单地打印了内存中该空间之前的任何垃圾值。

由于 C 旨在成为一种干净且高效的语言,因此它不会自动填充值,它只是分配一定的内存量。这部分内存将保留其先前的值,直到您将其初始化为其他内容为止。

关于c - 指针和地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24618945/

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