gpt4 book ai didi

c - "de-referencing a NULL pointer"到底是什么意思?

转载 作者:太空狗 更新时间:2023-10-29 16:18:21 26 4
gpt4 key购买 nike

我完全是 C 语言的新手,在我的大学工作期间,我遇到过代码中的注释,这些注释经常提到取消引用 NULL 指针。我确实有 C# 背景,我一直认为这可能类似于您在 .Net 中获得的“NullReferenceException”,但现在我有严重的怀疑。

有人可以通俗地向我解释一下这是什么以及为什么不好吗?

最佳答案

NULL 指针指向不存在的内存。这可能是地址 0x00000000 或任何其他实现定义的值(只要它永远不可能是真实地址)。取消引用它意味着尝试访问指针指向的任何内容。 * 运算符是取消引用运算符:

int a, b, c; // some integers
int *pi; // a pointer to an integer

a = 5;
pi = &a; // pi points to a
b = *pi; // b is now 5
pi = NULL;
c = *pi; // this is a NULL pointer dereference

这与 C# 中的 NullReferenceException 完全相同,只是 C 中的指针可以指向任何数据对象,甚至是数组中的元素。

关于c - "de-referencing a NULL pointer"到底是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4007268/

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