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

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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题