C 指针和地址?

C 指针和地址?,c,pointers,C,Pointers,我输入了以下内容: #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

我输入了以下内容:

#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;
}
#包括
int main()
{
int*ptr,x,y;
ptr=&x;
y=x;
printf(“这是x的位置上的值:%p\n”,*ptr);
printf(“这是x的地址:%p\n”,&x);
printf(“x的值是*ptr是:%i%x\n”,*ptr);
printf(“这是ptr的地址:%p\n”,&ptr);
printf(“y的值为:%i\n”,y);
printf(“y的地址是%p\n”,&y);
返回0;
}
控制台显示以下内容:

#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;
}
这是x:000004F的地址
这是x:0028FF18的地址
x的值为*ptr为:79 28ff94
这是ptr的地址:0028FF1C
y的值是0028FF14

我可以理解所有的printf结果,但除了第2个值的第3行之外
28ff94
? 我知道
*ptr=x
的第一个值
%d79
%x000004F

我明确没有指定第二个值,为空。但现在我想知道十六进制值
28ff94
从何而来?

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


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

您是否忘记为
x
赋值?否。所有内容都是有意编写的。我想知道为什么28ff94会代表?修复您的输出,您的程序无法生成此输出。谢谢Magister!。指定的位置也是任意的吗?随机的还是遵循某些规则?@viktorkh:这取决于具体的实现,但通常有定义良好的规则来留出存储空间。Google“stack frame”来查看典型的方法。确实,它是非常具体的实现,但我想让你看看Google的“寻址模式”,因为这是一个相当广泛的主题,专门讨论如何确定内存地址,这是非常有趣的。