gpt4 book ai didi

C 编程段错误 : 11

转载 作者:行者123 更新时间:2023-11-30 19:16:49 26 4
gpt4 key购买 nike

#include <stdio.h>
int a;
int *b;
int **c;
int ***d;
int ****e;
// * is the "value of operator"
// * is also called the dereferencing // operator

int main(void) {
a = 25;
b = &a; // & is the "address of operator" c = &b;
d = &c;
e = &d;

printf("\n\n%d %d %d\n\n",*(&a),*b+**c, ***d*****e);

return 0;
}

当我运行它时,它显示 Segmentation failure: 11 how can i fix it 以及它的含义是什么?

最佳答案

我认为,在你的代码中

  c = &b;

部分被错误地注释掉了。因此,c 的值为 NULL(全局范围),并且该值被取消引用并在 printf() 参数中使用,该参数又可以调用undefined behaviour

Segmentation fault是未定义行为的副作用之一。基本上它表明存在一些内存访问冲突

仅供引用,C11 标准文档,章节 6.5.3.3

The unary * operator denotes indirection. If the operand points to a function, the result is a function designator; if it points to an object, the result is an lvalue designating the object. If the operand has type ‘‘pointer to type’’, the result has type ‘‘type’’. If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined.

关于无效值,来自同一章的脚注 102

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

解决方案:取消该部分的注释。

关于C 编程段错误 : 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29410538/

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