我正在使用 C 开发文件系统模拟器。 我的节点结构如下所示:
typedef struct node
{
char name[64];
char type;
struct node* child;
struct node* sibling;
struct node* parent;
}Node;
如果在树中找不到具有特定名称的节点,我的递归函数 findNode() 将返回 NULL。
Node* findNode(char* name, Node* start)
{
printf("inside findNode, looking for %s, starting at %s\n", name, start->name);
Node* current = start;
if(strcmp(name, current->name) == 0)
{
printf("inside findNoe, node found.\n");
return current;
}
if(current->sibling->name != 0)
{
return findNode(name, current->sibling);
}
if(current->child->name != 0)
{
return findNode(name, current->child);
}
printf("inside findNode, node not found.\n");
return 0;
}
在调用 findNode() 并将其与 0 进行比较时,我的程序出现段错误。
if(findNode(bname,current) != 0) //seg fault here
{
printf("Error: node with basename already exists under directory name.\n");
return;
}
我知道程序在未找到节点时已到达 findNode() 的末尾,因为它会打印“找不到节点”消息。
我的印象是当返回类型是指针时可以返回 0。我试过使用 NULL,并在比较之前将结果保存到 Node* 变量。都没有用。
谁能解释一下这是怎么回事?我真的很感激。提前致谢。
编辑:
代码现在如下所示。开始时有一个新的检查来查看 current
是否为 NULL,并且我已经停止尝试访问 child
和 sibling
名称。
Node* findNode(char* name, Node* start)
{
Node* current = start;
if(current == NULL)
{
printf("inside findNode, NULL passed in\n");
return NULL;
}
printf("inside findNode, looking for %s, starting at %s\n", name, current->name);
if(strcmp(name, current->name) == 0)
{
printf("inside findNode, node found.\n");
return current;
}
if(current->sibling != NULL && current->sibling != root)
{
return findNode(name, current->sibling);
}
if(current->child != NULL && current->sibling != root)
{
return findNode(name, current->child);
}
printf("inside findNode, node not found.\n");
return NULL;
}
首先测试:“/”和我的根节点,名称为“/”。 第二个测试:“你好”和我的根节点,名称“/”。不应该找到“你好”。
inside findNode, looking for /, starting at /
inside findNode, node found.
inside findNode, looking for hello, starting at /
inside findNode, node not found.
Segmentation fault
最佳答案
我刚刚编写了一个程序来测试您的代码。请注意,在 main 函数中,我刚刚初始化了一个 NULL 指针。
#include <iostream>
using namespace std;
typedef struct node
{
char name[64];
char type;
struct node* child;
struct node* sibling;
struct node* parent;
}Node;
Node* findNode(char* name, Node* start)
{
printf("inside findNode, looking for %s, starting at %s\n", name, start->name);
Node* current = start;
if (strcmp(name, current->name) == 0)
{
printf("inside findNoe, node found.\n");
return current;
}
if (current->sibling->name != 0)
{
return findNode(name, current->sibling);
}
if (current->child->name != 0)
{
return findNode(name, current->child);
}
printf("inside findNode, node not found.\n");
return 0;
}
int main()
{
Node *n = NULL;
char bname[] = "somename";
Node *current = n;
if (findNode(bname, current) != 0) //seg fault here
{
printf("Error: node with basename already exists under directory name.\n");
}
}
我得到的输出是(使用 Visual Studio 2015)
在 findNode 中,寻找某个名称,从 (null) 开始
正如 kaylum
指出的那样,这一行 printf("inside findNode, looking for %s, starting at %s\n", name, start->name);
导致段错误。原因是因为您正试图访问一个空指针。
首先您需要检查您访问的指针是否为 NULL。那么您必须访问它的属性或值。
我希望这是有道理的。
关于c - 从 C 函数返回空指针,比较段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35259356/