出现指针和指针函数级别不同的问题。(初学者,代码比较小白轻喷)
在每一个函数给指针赋值的时候都出现这个问题。
下面是警告内容warning: passing argument 1 of 'creation' from incompatible pointer type
顺便提问一下给指针分配空间的问题,头指针的初始化必须要分配空间么?
因为当我把NULL赋值给头指针后再试图让头指针的next指向链表的时候就会弹出程序停止的问题。如果程序中有发现其他问题也希望一并指出,谢谢各位
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct type
{
int num;
char name[20];
int age;
char sex[20];
char cla[20];
}elem;
typedef struct Node
{
elem stm;
struct Node *next;
}node;
node *creation(node *n)
{
n = (node*)malloc(sizeof(node));
n->next = NULL;
return n;
}
node *add(node *n, node *p)
{
node *s, *ss;
s = n;
ss = p;
while (s->next == ss)
{
if (s->next != NULL)
s = s->next;
if (s->next == NULL && ss->next == NULL)
s->next = ss;
}
return s;
}
int main()
{
node *p, *head, *n, *pp, *headd, *tot, *tothd, *scan;
char str[20];
n = head = headd = tothd = (node*)malloc(sizeof(node));
tot = (node*)malloc(40 * sizeof(node));
tothd->next = tot;
p = creation(&p);//赋值的时候有问题
pp = creation(&pp);//赋值的时候有问题
head->next = p;
headd->next = pp;
printf("The first data num name age sex class(num=-1 to end).\n");
while (scanf("%d", &p->stm.num)&&p->stm.num != -1)
{
n->next = p;
p->next = NULL;
p = (node*)malloc(sizeof(node));
scanf("%s", str);
strcpy(p->stm.name, str);
scanf("%d", &p->stm.age);
scanf("%s", str);
strcpy(p->stm.sex, str);
scanf("%s", str);
strcpy(p->stm.cla, str);
n = p;
}
free(n);
n = (node*)malloc(sizeof(node));
printf("The second data num name age sex class(num=-1 to end).\n");
while (scanf("%d", &pp->stm.num) && pp->stm.num != -1)
{
n->next = pp;
pp->next = NULL;
pp = (node*)malloc(sizeof(node));
scanf("%s", str);
strcpy(pp->stm.name, str);
scanf("%d", &pp->stm.age);
scanf("%s", str);
strcpy(pp->stm.sex, str);
scanf("%s", str);
strcpy(pp->stm.cla, str);
n = pp;
}
p = head->next;
pp = headd->next;
tot = add(&p, &pp);//赋值的时候有问题
scan = tothd->next;
while (scan->next != NULL)
{
printf("%d ", scan->stm.age);
}
}