简体   繁体   English

c程序中的typedef struct错误

[英]typedef struct error in c program

Thanks for sharing your knowladge:)! 谢谢你分享你的知识:)! I just wrote this c program in devc++ and I've got alot of errors about the typdef and the struct like: "invalid use of undefined type struct item'" for every line with "->" operator, "forward declaration of struct item'" for line 4 this is the code: 我刚用devc ++编写了这个c程序,我有很多关于typdef和struct的错误:“无效使用未定义类型struct item'" for every line with "->" operator, "forward declaration of struct item struct item'" for every line with "->" operator, "forward declaration of struct item ''对于第4行,这是代码:

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

typedef struct item* ptr;
typedef struct itme
{
        int data;
        ptr next;
}node;
void add2list(ptr*,int);
void freeList(ptr*);
int main()
{
    ptr H=NULL;
    ptr p3=H;
    int num;
    while (scanf("%d",&num)!=EOF)
          add2list(&H,num);

    while(p3)
    {
            printf("%d  ",p3->data);
            p3=p3->next;
    }
    printf("end\n");   
    freeList(&H);
    return 0;
}

void add2list(ptr* H, int num)
{
     ptr p1,p2,T;
     T=(ptr)malloc(sizeof(node));
     if(!T)
     {
           printf("cannot allocate memory\n");
           exit(0);
     }
     t->data=num;
     p1=*H;
     while(p1)
     {
           if(p1->data==num)
           {
              free(T);
              goto duplicate;
           }   
           else 
           {
                p2=p1;
                p1=p1->next;
           }
     }
     T->next=p1;
     if(p1==*H)
         *H=T; 
     else
     p2->next=T;
     duplicate:;
}   
void freeList(ptr* H)
{
     ptr p1;
     while(H)
     {
         p1=*H;
         (*H)=p1->next;
         free(p1);
     }

}

thanks! 谢谢!

The main issue is that you spelled "item" "itme". 主要问题是你拼写“item”“itme”。 Happens to the best of us. 发生在我们最好的人身上。

I did not looked all your code but it seems there is a typo 我没看过你所有的代码,但似乎有一个错字

typedef struct item* ptr; typedef struct item * ptr;

typedef struct itme { typedef struct itme {

Also this code snippet 这段代码也是如此

while(p3)
{
        printf("%d  ",p3->data);
        p3=p3->next;
}

has no sense because p3 was explicitly initialized by NULL 没有意义,因为p3是由NULL显式初始化的

ptr H=NULL;
ptr p3=H;

At least before the loop you should add statement 至少在循环之前你应该添加语句

p3 = H;

Also in function add2list there is another typo 另外在函数add2list还有另一个拼写错误

You declared pointer T but then further are using pointer t 你声明了指针T ,然后进一步使用指针t

void add2list(ptr* H, int num)
{
     ptr p1,p2,T;
     T=(ptr)malloc(sizeof(node));
     if(!T)
     {
           printf("cannot allocate memory\n");
           exit(0);
     }
     t->data=num;

Function freeList is also wrong. 函数freeList也错了。

Instead of statement 而不是声明

 while(H)

there shall be 应该有

 while( *H )

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

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