“取消引用指向不完整类型的指针”错误。有什么建议? [英] "dereferencing pointer to incomplete type" error. Any advice?

查看:104
本文介绍了“取消引用指向不完整类型的指针”错误。有什么建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

#define M 100
#define FLUSH while(getchar() != '\n')

typedef struct node *nd;

typedef struct 
{
       char bkNumber[5];
       char bkTitle[M];
       char bkAuthor[M];
       int bkCopyright;
       
}BOOK;

typedef struct
{
        BOOK rec;
        nd right;
        nd left;
}NODE;

BOOK getData(void);
void addNode(BOOK, nd *);

int main(void)
{
    nd root;
    BOOK data;
    int choice;
    
    printf("1] Add new book record \n2] Edit book title \n3] View all the books");
    printf("\n4] View a specific book \n5] Delete a book \n\n");
    printf("Choice: ");
    scanf("%d", &choice);
    
    system("cls");
    
    switch(choice)
    {
        case 1:
             data = getData();
             addNode(data, &root);
             break;
    }
    
    return 0;
}

BOOK getData(void)
{
     BOOK d;
     FLUSH;

     printf("Book Number: ");
     scanf("%4s", d.bkNumber);
     
     printf("Book Title: ");
     scanf("%s", d.bkTitle);
          
     printf("Author: ");
     scanf("%s", d.bkAuthor);

     printf("Copyright Year: ");
     scanf("%d", d.bkCopyright);
     
     return d;
}

void addNode(BOOK data, nd *root)
{
     nd temp, tp, tp1;
     
        temp = malloc(sizeof(NODE));
        
        strcpy(temp->rec.bkNumber, data.bkNumber);
        temp->right = NULL;
        temp->left = NULL;
        
        if(root == NULL)
                *root = temp;
        else
        {
            tp1 = *root;
            
            while(tp1 != NULL)
            {
                      tp = tp1;
                      if(temp->rec <= tp1->rec)
                          tp1 = tp1->rec;
                      else
                          tp1 = tp1->right;
            }//end of while
            
            if(temp->rec <= tp->rec)
                      tp->left = temp;
            else
                      tp->right = temp;
        }//end of else
        
        tp = NULL;
        tp1 = NULL;
        
        return;
}//end of addNode

推荐答案

所有代码都引用一个变量类型nd,定义如下:

All your code refers to a variable type nd defined as:
typedef struct node *nd;



在这种情况下,nd定义了一个指向'不完整'的指针'节点结构。

结构'node'是无处定义的,所以当你想要访问结构的字段时,编译器不知道如何获取它们(结构不完整,没有字段在任何地方定义) :))。

也许你会引用一个'NODE'结构定义为:


In this case nd defines a pointer to an 'incomplete' node structure.
The structure 'node' is defined nowhere, so when you want access fields of the structure the compiler doesn't know how to get them (the structure is incomplete no fields are defined anywhere :)).
Perhaps you would refer to a 'NODE' structure defined as:

typedef struct NODE *nd;



在这种情况下,你犯了一个拼写错误;)

为了避免这个问题,最好总是在同一行上定义结构及其所有引用类型:


In this case you made a typo error ;)
To avoid the problem it's better to always define structures and all their reference types on the same line:

typedef struct
{
        BOOK rec;
        nd right;
        nd left;
}NODE, *nd;         //define *nd here



至少会避免拼写错误,因为你不必重写参考文件。



PS我假设您知道C是区分大小写的语言,这意味着'node'和'NODE'相同。


这篇关于“取消引用指向不完整类型的指针”错误。有什么建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆