# 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