gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:43:19 24 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/45358666/

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