繁体   English   中英

从函数返回NULL的指针数组

[英]Array of pointers returning NULL from function

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

这是基本的节点树设置,可以将节点彼此添加以创建树状结构。 创建和添加节点时,一切正常。

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

我正在Linux Mint 19上的C语言中进行编译。我提供了标头,代码和一个基本示例。

---- ----头

#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函数中。 当节点传递给它时,子节点现在变为空(也称为0)。

这是我用来存储和访问其他库中的指针数组的确切方法。 也许我缺少一些简单的东西。 无论哪种方式,它总是有助于获得外界新鲜的见解。

在打印循环中

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

必定是

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

(也许有休息?;>)


无关,但严格来说,将p转换说明符与void指针一起使用时,会调用未定义的行为。

所以最好做:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM