C语言 建立栈报错 error: dereferencing pointer to incomplete type

来源:互联网 发布:洪博培事件知乎 编辑:程序博客网 时间:2024/05/17 18:00

# include <stdio.h>
# include <stdlib.h>
# include <time.h>
struct stacknode
{
        int data;
        struct stacknode *next;
};
struct stacknode *push(int n )
{
        struct stactnode *top,*s;
        top=NULL;
        srand((unsigned)time(NULL));
        int i;
        for ( i =0;i<n;i++)
        {
                 

        s = (struct stacknode *)malloc(sizeof(struct stacknode));
        s->data = rand()%100+1;
        s->next = top;
        top =s ;
        }
        return top;
}

int main (void)
{
int n;
struct stacknode *p;
printf("Enter a number n : ");
scanf("%i",&n);
p = push(n);

while (p!=NULL)
{
        printf("%4i",p->data);
        p=p->next;
}
printf("\n");
return 0;
}

 

编译出错:

[root@localhost Gcc]# gcc Stack.c
Stack.c: In function ?.ush?.
Stack.c:18: warning: assignment from incompatible pointer type
Stack.c:19: error: dereferencing pointer to incomplete type
Stack.c:20: error: dereferencing pointer to incomplete type
Stack.c:23: warning: return from incompatible pointer type
            

解决方法:

这是定义的时候出错,
        struct stactnode *top,*s; 应该是struct stacknode

0 0
原创粉丝点击