夜幕而已 2019-05-11 14:35 采纳率: 0%
浏览 1150
已采纳

C语言 间接级别不同(指针和指针函数方面)

出现指针和指针函数级别不同的问题。(初学者,代码比较小白轻喷)

在每一个函数给指针赋值的时候都出现这个问题。

下面是警告内容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);
    }
}
  • 写回答

2条回答 默认 最新

  • coderhss 2019-05-11 15:27
    关注

    creation的形参不需要加取地址符&

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记