struct变为NULL

时间:2015-03-20 02:17:45

标签: c struct tree linked-list

我想实现类似OOP的链表实现。

#include <ctype.h>
#include <string.h>
#include <stdlib.h>

typedef struct node {
    char data[100];
    struct node *next;
    struct node *prev;
}Node;

typedef struct treeNode {
    char treeData[100];
    struct treeNode *left;
    struct treeNode *right;
}TreeNode;

typedef struct sql_struct{
    Node *head, *tail;
}StackQueueList;

typedef struct tree {
    TreeNode *root;
}Tree;

void initStackQueueList(StackQueueList* a) {
    a->head = a->tail = NULL;
}///initStackQueueList

Tree* initTree (Tree* a) {
    a->root = NULL;
}///newTree

但问题是每当我使用initStackQueueList()它总是让指针似乎是NULL哪个有意义,因为我希望它里面的VALUES为空,只是不是POINTER本身是空值。所以可能,我在这里实施了一些错误,我无法绕过它,所以我在寻求帮助! :)

示例实施:

static const StackQueueList *dqFriend1, *dqFriend2, *dqMyself, *dqVirus;

void initQueues () {
    initStackQueueList(dqFriend1);
    initStackQueueList(dqFriend2);
    initStackQueueList(dqMyself);
    initStackQueueList(dqVirus);
}

=更正版本=

StackQueueList *dqFriend1, *dqFriend2, *dqMyself, *dqVirus;
*dqFriend1 = *dqFriend2 = *dqMyself = *dqVirus = (StackQueueList*) malloc(sizeof (StackQueueList));

void initQueues () {
    initStackQueueList(dqFriend1);
    initStackQueueList(dqFriend2);
    initStackQueueList(dqMyself);
    initStackQueueList(dqVirus);
}

但现在我遇到了这些错误:

error: conflicting types for 'dqFriend1'|
error: incompatible types when assigning to type 'StackQueueList' from type 'struct StackQueueList *'|

1 个答案:

答案 0 :(得分:1)

您应首先为指针dqFriend1dqFriend2dqMyselfdqVirus分配空间。否则他们的成员就没有地方。

eg. dqFriend1 = (StackQueueList*)malloc(sizeof(StackQueueList));