取消引用指向不完整类型的指针(基数树)
dereferencing pointer to incomplete type (radix tree)
我的结构定义遇到了很大的问题。我尝试了几种不同的方法来定义它们,但似乎无法消除错误。
我的代码可能还有很多其他问题,但如果不通过 运行 我认为的代码找到它们,我实际上无法解决这些问题。这就是为什么我需要先解决这个问题。
完整代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int bool;
enum { false, true };
typedef struct radixNode {
bool active;
struct node * pnt;
struct node * l;
struct node * r;
} node;
void insert(node *root, char * B) {
// digit is zero so we go left
if (B[0] == 0) {
// left child doesn't exist, create it
if (root->l == NULL) {
root->l = malloc(sizeof(node));
/* if the next index in the string does NOT contain a 1 or 0,
the current index is the last index and the node is activated */
if (B[1] == 1 || B[1] == 0)
root->l->active = false;
else
root->l->active = true;
root->l->pnt = root;
root->l->l = NULL;
root->l->r = NULL;
insert(root->l,B++); // B++ removes the first digit of the string
}
// left child exists, traverse
else {
insert(root->l,B++);
}
}
// digit is one, go right
else {
// right child doesn't exist, create it
if (root->r == NULL) {
root->r = malloc(sizeof(node));
/* if the next index in the string does NOT contain a 1 or 0,
the current index is the last index and the node is activated */
if (B[1] == 1 || B[1] == 0)
root->r->active = false;
else
root->r->active = true;
root->r->pnt = root;
root->r->l = NULL;
root->r->r = NULL;
insert(root->r,B++);
}
// left child exists, traverse
else {
insert(root->r,B++);
}
}
}
node * printTreeMin(node *root) {
char * C[10];
/* goes left until it can't, appends 0 to string
till it can't. if node is active, print the string */
while (root->l != NULL) {
C[strlen(C)] = '0';
if (root->active)
printf("&s\n",C);
root = root->l;
}
return root;
}
// prints the next smallest binary number in the tree, returns the node it printed
node * printNextSmallest(node * root) {
char * C[10];
// if right child exists, go there and find lowest node (after if same deal as printTreeMin() )
if (root->r != NULL) {
C[strlen(C)] = '1';
if (root->active)
printf("&s\n",C);
root = root->r;
while (root->l != NULL) {
C[strlen(C)] = '0';
if (root->active)
printf("&s\n",C);
root = root->l;
}
return root;
}
node * temp = root->pnt;
while (temp != NULL && root == temp->r) {
root = temp;
temp = temp->pnt;
}
return temp;
}
void printRadixTree(node *root) {
root = printTreeMin(root);
while (printNextSmallest(root) != NULL)
root = printNextSmallest(root);
}
void test() {
node * tree = malloc(sizeof(node));
tree->l = NULL;
tree->r = NULL;
// a)
insert(tree,"101000");
insert(tree,"10100");
insert(tree,"10110");
insert(tree,"101");
insert(tree,"1111");
// b)
printRadixTree(tree);
}
int main() {
test();
}
这是我得到的错误:
|In function 'insert':|
30|error: dereferencing pointer to incomplete type|
32|error: dereferencing pointer to incomplete type|
34|error: dereferencing pointer to incomplete type|
35|error: dereferencing pointer to incomplete type|
36|error: dereferencing pointer to incomplete type|
37|warning: passing argument 1 of 'insert' from incompatible pointer type [enabled by default]|
17|note: expected 'struct node *' but argument is of type 'struct node *'|
42|warning: passing argument 1 of 'insert' from incompatible pointer type [enabled by default]|
17|note: expected 'struct node *' but argument is of type 'struct node *'|
57|error: dereferencing pointer to incomplete type|
59|error: dereferencing pointer to incomplete type|
61|error: dereferencing pointer to incomplete type|
62|error: dereferencing pointer to incomplete type|
63|error: dereferencing pointer to incomplete type|
64|warning: passing argument 1 of 'insert' from incompatible pointer type [enabled by default]|
17|note: expected 'struct node *' but argument is of type 'struct node *'|
69|warning: passing argument 1 of 'insert' from incompatible pointer type [enabled by default]|
17|note: expected 'struct node *' but argument is of type 'struct node *'|
|In function 'printTreeMin':|
82|warning: passing argument 1 of 'strlen' from incompatible pointer type [enabled by default]|
49|note: expected 'const char *' but argument is of type 'char **'|
82|warning: assignment makes pointer from integer without a cast [enabled by default]|
87|warning: assignment from incompatible pointer type [enabled by default]|
|In function 'printNextSmallest':|
101|warning: passing argument 1 of 'strlen' from incompatible pointer type [enabled by default]|
49|note: expected 'const char *' but argument is of type 'char **'|
101|warning: assignment makes pointer from integer without a cast [enabled by default]|
105|warning: assignment from incompatible pointer type [enabled by default]|
109|warning: passing argument 1 of 'strlen' from incompatible pointer type [enabled by default]|
49|note: expected 'const char *' but argument is of type 'char **'|
109|warning: assignment makes pointer from integer without a cast [enabled by default]|
113|warning: assignment from incompatible pointer type [enabled by default]|
119|warning: initialization from incompatible pointer type [enabled by default]|
121|warning: comparison of distinct pointer types lacks a cast [enabled by default]|
124|warning: assignment from incompatible pointer type [enabled by default]|
||=== Build failed: 10 error(s), 16 warning(s) (0 minute(s), 0 second(s)) ===|
如评论中所见,您的问题出在您的结构定义上:
typedef struct radixNode {
bool active;
struct node * pnt;
struct node * l;
struct node * r;
} node;
您在 typedef 结束前调用 struct node
。只需将 struct node
替换为 struct radixNode
.
对于你的第二个问题,这是因为你的变量 C 是一个指针数组,而不是一个字符数组,而你正试图将一行分配为一个字符。如果要创建一个十字节长的字符数组,只需这样定义它:
char C[10];
顺便说一句,如果你想用 printf 打印一个字符串,它是 %s
,而不是 &s
。
我的结构定义遇到了很大的问题。我尝试了几种不同的方法来定义它们,但似乎无法消除错误。
我的代码可能还有很多其他问题,但如果不通过 运行 我认为的代码找到它们,我实际上无法解决这些问题。这就是为什么我需要先解决这个问题。
完整代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int bool;
enum { false, true };
typedef struct radixNode {
bool active;
struct node * pnt;
struct node * l;
struct node * r;
} node;
void insert(node *root, char * B) {
// digit is zero so we go left
if (B[0] == 0) {
// left child doesn't exist, create it
if (root->l == NULL) {
root->l = malloc(sizeof(node));
/* if the next index in the string does NOT contain a 1 or 0,
the current index is the last index and the node is activated */
if (B[1] == 1 || B[1] == 0)
root->l->active = false;
else
root->l->active = true;
root->l->pnt = root;
root->l->l = NULL;
root->l->r = NULL;
insert(root->l,B++); // B++ removes the first digit of the string
}
// left child exists, traverse
else {
insert(root->l,B++);
}
}
// digit is one, go right
else {
// right child doesn't exist, create it
if (root->r == NULL) {
root->r = malloc(sizeof(node));
/* if the next index in the string does NOT contain a 1 or 0,
the current index is the last index and the node is activated */
if (B[1] == 1 || B[1] == 0)
root->r->active = false;
else
root->r->active = true;
root->r->pnt = root;
root->r->l = NULL;
root->r->r = NULL;
insert(root->r,B++);
}
// left child exists, traverse
else {
insert(root->r,B++);
}
}
}
node * printTreeMin(node *root) {
char * C[10];
/* goes left until it can't, appends 0 to string
till it can't. if node is active, print the string */
while (root->l != NULL) {
C[strlen(C)] = '0';
if (root->active)
printf("&s\n",C);
root = root->l;
}
return root;
}
// prints the next smallest binary number in the tree, returns the node it printed
node * printNextSmallest(node * root) {
char * C[10];
// if right child exists, go there and find lowest node (after if same deal as printTreeMin() )
if (root->r != NULL) {
C[strlen(C)] = '1';
if (root->active)
printf("&s\n",C);
root = root->r;
while (root->l != NULL) {
C[strlen(C)] = '0';
if (root->active)
printf("&s\n",C);
root = root->l;
}
return root;
}
node * temp = root->pnt;
while (temp != NULL && root == temp->r) {
root = temp;
temp = temp->pnt;
}
return temp;
}
void printRadixTree(node *root) {
root = printTreeMin(root);
while (printNextSmallest(root) != NULL)
root = printNextSmallest(root);
}
void test() {
node * tree = malloc(sizeof(node));
tree->l = NULL;
tree->r = NULL;
// a)
insert(tree,"101000");
insert(tree,"10100");
insert(tree,"10110");
insert(tree,"101");
insert(tree,"1111");
// b)
printRadixTree(tree);
}
int main() {
test();
}
这是我得到的错误:
|In function 'insert':|
30|error: dereferencing pointer to incomplete type|
32|error: dereferencing pointer to incomplete type|
34|error: dereferencing pointer to incomplete type|
35|error: dereferencing pointer to incomplete type|
36|error: dereferencing pointer to incomplete type|
37|warning: passing argument 1 of 'insert' from incompatible pointer type [enabled by default]|
17|note: expected 'struct node *' but argument is of type 'struct node *'|
42|warning: passing argument 1 of 'insert' from incompatible pointer type [enabled by default]|
17|note: expected 'struct node *' but argument is of type 'struct node *'|
57|error: dereferencing pointer to incomplete type|
59|error: dereferencing pointer to incomplete type|
61|error: dereferencing pointer to incomplete type|
62|error: dereferencing pointer to incomplete type|
63|error: dereferencing pointer to incomplete type|
64|warning: passing argument 1 of 'insert' from incompatible pointer type [enabled by default]|
17|note: expected 'struct node *' but argument is of type 'struct node *'|
69|warning: passing argument 1 of 'insert' from incompatible pointer type [enabled by default]|
17|note: expected 'struct node *' but argument is of type 'struct node *'|
|In function 'printTreeMin':|
82|warning: passing argument 1 of 'strlen' from incompatible pointer type [enabled by default]|
49|note: expected 'const char *' but argument is of type 'char **'|
82|warning: assignment makes pointer from integer without a cast [enabled by default]|
87|warning: assignment from incompatible pointer type [enabled by default]|
|In function 'printNextSmallest':|
101|warning: passing argument 1 of 'strlen' from incompatible pointer type [enabled by default]|
49|note: expected 'const char *' but argument is of type 'char **'|
101|warning: assignment makes pointer from integer without a cast [enabled by default]|
105|warning: assignment from incompatible pointer type [enabled by default]|
109|warning: passing argument 1 of 'strlen' from incompatible pointer type [enabled by default]|
49|note: expected 'const char *' but argument is of type 'char **'|
109|warning: assignment makes pointer from integer without a cast [enabled by default]|
113|warning: assignment from incompatible pointer type [enabled by default]|
119|warning: initialization from incompatible pointer type [enabled by default]|
121|warning: comparison of distinct pointer types lacks a cast [enabled by default]|
124|warning: assignment from incompatible pointer type [enabled by default]|
||=== Build failed: 10 error(s), 16 warning(s) (0 minute(s), 0 second(s)) ===|
如评论中所见,您的问题出在您的结构定义上:
typedef struct radixNode {
bool active;
struct node * pnt;
struct node * l;
struct node * r;
} node;
您在 typedef 结束前调用 struct node
。只需将 struct node
替换为 struct radixNode
.
对于你的第二个问题,这是因为你的变量 C 是一个指针数组,而不是一个字符数组,而你正试图将一行分配为一个字符。如果要创建一个十字节长的字符数组,只需这样定义它:
char C[10];
顺便说一句,如果你想用 printf 打印一个字符串,它是 %s
,而不是 &s
。