gounie
gounie
2020-02-19 17:53
采纳率: 60%
浏览 1.2k

如何返回结构体指针?

小白新学链表 想用函数来表达一下 在函数返回值的地方遇到麻烦 请大佬们帮助

typedef struct student {
    char name[10];
    int num;
    struct scan {
        int speach;
        int read;
        int write;
        int hear;
    }SCAN;
    struct student *next;
}STUDENT;

用函数创建一个链表后 想返回头节点 如何以STUDENT* 返回函数值

STUDENT* creatlink() {
    char name[10],*t;
    int i, n, num, s, r, w, h;
    STUDENT *head, *p, *q;
    printf("输入要录入的人数:");
    scanf("%d", &n);
    printf("请依次录入学生的学号 姓名 及听说读写的分数");
    head = NULL;
    q = NULL;
    for (i = 0; i < n; i++) {
        p = (STUDENT *)malloc(sizeof(STUDENT));
        scanf("%d%s%d%d%d%d", &num, name, &s, &r, &w, &h);
        p->num = num;
        strcpy(p->name,name);
        p->SCAN.speach = s;
        p->SCAN.hear = h;
        p->SCAN.read = r;
        p->SCAN.write = w;
        if (head == NULL) head = p;
        else q->next = p;
        q = p;
    }

    return *head;
}

编译时在
return head; *号上
说 不存在从 STUDENT 到STUDENT
的适当转换函数
然后我删掉返回值上的*(return *head;) 后 输出的函数又不能用了

求大佬帮助 全部代码在下面

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct student {
    char name[10];
    int num;
    struct scan {
        int speach;
        int read;
        int write;
        int hear;
    }SCAN;
    struct student *next;
}STUDENT;
STUDENT* creatlink() {
    char name[10],*t;
    int i, n, num, s, r, w, h;
    STUDENT *head, *p, *q;
    printf("输入要录入的人数:");
    scanf("%d", &n);
    printf("请依次录入学生的学号 姓名 及听说读写的分数");
    head = NULL;
    q = NULL;
    for (i = 0; i < n; i++) {
        p = (STUDENT *)malloc(sizeof(STUDENT));
        scanf("%d%s%d%d%d%d", &num, name, &s, &r, &w, &h);
        p->num = num;
        strcpy(p->name,name);
        p->SCAN.speach = s;
        p->SCAN.hear = h;
        p->SCAN.read = r;
        p->SCAN.write = w;
        if (head == NULL) head = p;
        else q->next = p;
        q = p;
    }

    return *head;
}

void putlink(STUDENT *HEAD) {
    STUDENT *t;
    t = HEAD;
    while (t != NULL) {
        printf("%d\t%s\t听%d,说%d,读%d,写%d", t->num, t->name, t->SCAN.hear, t->SCAN.speach, t->SCAN.read, t->SCAN.write);
        t = t + 1;
    }

}

int main() {
    STUDENT *HEAD;
    HEAD = NULL;



    putlink(creatlink());
    return 0;
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • sun_xf1
    sun_xf1 2020-02-19 20:01
    已采纳

    STUDENT* creatlink() {
    char name[10],*t;
    int i, n, num, s, r, w, h;
    STUDENT *head, *p, *q;
    printf("输入要录入的人数:");
    scanf("%d", &n);
    printf("请依次录入学生的学号 姓名 及听说读写的分数");
    head = NULL;
    q = NULL;
    for (i = 0; i < n; i++) {
    p = (STUDENT *)malloc(sizeof(STUDENT));
    scanf("%d%s%d%d%d%d", &num, name, &s, &r, &w, &h);
    p->num = num;
    strcpy(p->name,name);
    p->SCAN.speach = s;
    p->SCAN.hear = h;
    p->SCAN.read = r;
    p->SCAN.write = w;
    if (head == NULL)
    head = p;

        if(q != NULL)
          q->next = p;
        q = p;
    }
    
    return head;
    

    }

    点赞 评论

相关推荐