c - 从函数返回 NULL 的指针数组

标签 c arrays pointers null

我很难追踪节点结构中指针返回 null 的问题。我将程序简化为最基本的功能,以便您可以准确地看到问题所在。

这是一个基本的节点树设置,其中节点可以相互添加以创建类似树的结构。创建和添加节点时一切正常。

我有一个打印函数,它可以打印节点名称及其子节点的名称,这就是问题所在。每当我将一个节点传递给函数时,指向其子节点的指针总是返回 null。但是,我可以访问子指针,当直接从父级访问时不返回 null,它们仅在将父级复制到函数中时返回 null。

我正在 Linux Mint 19 上用 C 进行编译。我包含了 header 、代码和基本示例。

----标题----

#ifndef node_h
#define node_h

typedef struct Node node;

struct Node {
    node* parent;
    node** children;
    char* name;
    int count;
};

node* create_node(char* name);

void add_child_node(node* parent, node* child);

void print_node(node* n);

#endif

----c文件----

#include <stdio.h>
#include <stdlib.h>
#include "node.h"

node* create_node(char* name) {
    node* n = malloc(sizeof(node));
    if (n == 0) {
        printf("Unable to create node %s.\n", name);
        exit(1);
    }
    n->name = name;
    n->parent = 0;
    n->children = 0;
    //n->map = create_stringmap();
    n->count = 0;
    return n;
}

void add_child_node(node* parent, node* child) {
    child->parent = parent;
    int count = parent->count + 1;
    parent->children = realloc(parent->children, sizeof(node) * count);
    if (parent->children == 0) {
        printf("Unable to add child node %s to parent %s.\n", child->name, parent->name);
        exit(1);
    }
    if (child == 0) {
        printf("The child node you are attempting to add to %s is null.\n", parent->name);
        exit(1);
    }
    // Add child to end of array.
    parent->children[parent->count] = child;
    parent->count = count;
}

void print_node(node* n) {
    //print_node_path(n);
    printf("%s\n", n->name);
    int count = n->count;
    for (int i = 0; i < count; i++) {
        printf("%p\n", n->children[count]);
        printf("%s\n", n->children[count]->name); // Crashes because children are null.
        //print_node(n->children[count]); 
    }
}

----测试文件----

#include <stdio.h>
//#include <unistd.h>
#include <stdlib.h>
#include "node.h"

// gcc -Wall node.c stringstest.c -o stringstest && ./stringstest

int main() {

//while (1) {

// Create tree
node* root = create_node("root");
node* branch1 = create_node("branch1");
node* branch2 = create_node("branch2");

// Add children
add_child_node(root, branch1);
add_child_node(root, branch2);

printf("%s\n" , root->children[0]->name);
printf("%s\n" , root->children[1]->name);
printf("%p\n", root->children[0]);
printf("%p\n", root->children[1]);
print_node(root); // Crashes because children are return null.
// This works since no children are accessed in the loop.
//print_node(branch2);

//usleep(100);
//}

return 0;

}

在示例中,直接访问时,子节点工作正常,问题出在 print_node 函数中。当节点传递给它时,子节点现在变为 null 也称为 0。

这正是我用来存储和访问其他库中的指针数组的方法。也许我错过了一些简单的事情。无论哪种方式,获得外部的新鲜视角总是有帮助的。

最佳答案

打印循环内部 这个

    printf("%p\n", n->children[count]);

应该是

    printf("%p\n", n->children[i]);

(也许休息一下?;>)

<小时/>

不相关,但严格来说,将 p 转换说明符与 void 指针以外的其他内容一起使用会调用未定义的行为。

所以最好这样做:

    printf("%p\n", (void*) n->children[i]);

关于c - 从函数返回 NULL 的指针数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54378883/

相关文章:

c++ - 错误 : expected primary-expression before & token

javascript - 如何将数据推送到位置为[i]的数组?

c++ - 对结构数组使用构造函数

c++ - 如何修复虚幻引擎4中的 "Pointer to Incomplete class type is not allowed"错误

c - GLFW3 : warning: implicit declaration of function ‘glGenBuffers’

c - BullsEye代码覆盖率

c - C 中的运行时多态性

在 C 中将二进制文件转换为文本文件

javascript - 从隐藏值创建 Javascript 数组

C++ 转换错误