gpt4 book ai didi

c - 用指针访问零位

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

为什么我们不能访问例如位置零(0),一百(100),内存中有两百(200);

例如:

void main() {
int* a;
a = 0;
printf("what is inside position o %d",*a);

}

最佳答案

引用 C11,章节 §6.5.3.2p4

... If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined.

引用 C11,章节 §note102

... Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer, an address ...

强调我的

此外,

引用 C11,章节 §6.3.2.3p3

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

因此,您正在尝试取消引用一个空指针,其行为已明确表示为标准未定义。

关于c - 用指针访问零位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52879335/

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