gpt4 book ai didi

c - C 中棘手的段错误

转载 作者:太空狗 更新时间:2023-10-29 15:50:28 25 4
gpt4 key购买 nike

我正在尝试为我的大学作业运行一个 C 项目,但我在以下代码段中的“while (current->next != NULL) {”行遇到了段错误:

FILE* f = fileOpen("test.txt");
if (f != NULL){
functionList = fileReadToMemory(f, &graphParams);//functionList is a pointer to the first value of the linked list it creates
current = functionList;

while (current->next != NULL) {
printf("%d %d %d %s", current->red, current->green, current->blue, current->expression);//Prints value of linked list
current = current -> next;
}
}

gdb报错如下:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x000000000000003a
0x0000000100000b30 in main () at main.c:23
23 while (current->next != NULL) {

我做错了什么?

提前致谢!

最佳答案

你需要做的

while (current != NULL) 

代替

current->next != NULL 

因为列表中的最后一个元素会导致段错误。

关于c - C 中棘手的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26559112/

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