小编Cyn*_*ode的帖子

将指针解引用到任意地址会导致分段错误

我已经为指针编写了一个简单的C代码。根据我的理解,Pointer是一个变量,其中包含另一个变量的地址。例如:

 int x = 25; // address - 1024
 int *ptr = &x;
 printf("%d", *ptr); // *ptr will give value at address of x i.e, 25 at 1024 address.
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试下面的代码时,我得到了分段错误

#include "stdio.h"
int main()
{
    int *ptr = 25;
    printf("%d", *ptr);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

怎么了 为什么指针变量不能返回地址25的值?我不应该能够读取该地址上的字节吗?

c pointers undefined-behavior dereference

1
推荐指数
2
解决办法
103
查看次数

标签 统计

c ×1

dereference ×1

pointers ×1

undefined-behavior ×1