鱼C论坛

 找回密码
 立即注册
查看: 1693|回复: 2

[已解决]求教,这样写返回的pt->score像是个地址,哪里出错了,请高手指点

[复制链接]
发表于 2021-4-20 20:39:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(Stu)    //宏替换 LEN代表struct student
                                                                          //类型数据的长度 sizeof是 求字节数运算符

typedef struct Student
{
        long num;
        float score;
        struct Student* next;
}Stu;

int n;  //n为全局变量,本文件中各模块均可使用它
Stu* creat(void)     //定义结构函数,此函数返回一个指向 链表头的指针
{
        Stu* head;
        Stu* p1, * p2;
        n = 0;
        p1 = p2 = (Stu*)malloc(LEN);    //开辟一个新单元  malloc需要头文件#include(stdlib.h)  内存分配函数
        scanf("%ld,%f", &p1->num, &p1->score); //输入第一个学生的学号和成绩
        head = NULL;

        while (p1->num != 0)
        {
                //n++;
                n = n + 1;
                if (n == 1)
                {
                        head = p1;
                }
                else
                {
                        p2->next = p1;
                }
               
                p2 = p1;
                p1 = (Stu*)malloc(LEN);     //开辟动态存储区,把起始地址赋给p1
                scanf("%ld,%f", &p1->num, &p1->score);   //输入其他学生的学号和成绩
                //char c;
                //while ((c = getchar()) != '\n');
        }
        p2->next = NULL;
        return head;
}
int main()
{
        Stu* pt;
        pt = creat();  //函数返回链表第一个结点的地址
        printf("\n num:%ld\n score:%5.1f\n", pt->num, pt->score);  //输出第一个结点的成员值
        //free(Stu);  //释放结构体
        return 0;
}
最佳答案
2021-4-20 21:10:12
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #define LEN sizeof(Stu)    //宏替换 LEN代表struct student
  5. //类型数据的长度 sizeof是 求字节数运算符

  6. typedef struct Student
  7. {
  8.     long num;
  9.     float score;
  10.     struct Student* next;
  11. }Stu;

  12. int n;  //n为全局变量,本文件中各模块均可使用它
  13. Stu* creat(void)     //定义结构函数,此函数返回一个指向 链表头的指针
  14. {
  15.     Stu* head;
  16.     Stu* p;         /*------------不需要那么多指针---------------*/
  17.     n = 0;
  18.     p = (Stu*)malloc(LEN);    //开辟一个新单元  malloc需要头文件#include(stdlib.h)  内存分配函数
  19.     if (!p)
  20.     {
  21.         printf_s("内存申请失败\n");
  22.         return NULL;
  23.     }

  24.     scanf_s("%ld%*c%f", &p->num, &p->score); //输入第一个学生的学号和成绩
  25.     head = NULL;

  26.     while (p->num != 0)
  27.     {
  28.         n = n + 1;
  29.         if (n == 1)
  30.             head = p;/*------------第一次时赋值给head---------------*/

  31.         p = p->next;/*------------以后每次指向自身的下个节点---------------*/

  32.         p = (Stu*)malloc(LEN);     //开辟动态存储区,把起始地址赋给p1
  33.         if (!p)
  34.         {
  35.             printf_s("内存申请失败\n");
  36.             return head;
  37.         }

  38.         scanf_s("%ld%*c%f", &p->num, &p->score);   //输入其他学生的学号和成绩
  39.     }
  40.    
  41.     return head;
  42. }
  43. int main()
  44. {
  45.     Stu* pt;
  46.     pt = creat();  //函数返回链表第一个结点的地址
  47.     printf("\n num:%ld\n score:%5.1f\n", pt->num, pt->score);  //输出第一个结点的成员值
  48.     return 0;
  49. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-4-20 21:05:06 | 显示全部楼层
己解决,,,是输入法的问题个人失误
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-20 21:10:12 | 显示全部楼层    本楼为最佳答案   
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #define LEN sizeof(Stu)    //宏替换 LEN代表struct student
  5. //类型数据的长度 sizeof是 求字节数运算符

  6. typedef struct Student
  7. {
  8.     long num;
  9.     float score;
  10.     struct Student* next;
  11. }Stu;

  12. int n;  //n为全局变量,本文件中各模块均可使用它
  13. Stu* creat(void)     //定义结构函数,此函数返回一个指向 链表头的指针
  14. {
  15.     Stu* head;
  16.     Stu* p;         /*------------不需要那么多指针---------------*/
  17.     n = 0;
  18.     p = (Stu*)malloc(LEN);    //开辟一个新单元  malloc需要头文件#include(stdlib.h)  内存分配函数
  19.     if (!p)
  20.     {
  21.         printf_s("内存申请失败\n");
  22.         return NULL;
  23.     }

  24.     scanf_s("%ld%*c%f", &p->num, &p->score); //输入第一个学生的学号和成绩
  25.     head = NULL;

  26.     while (p->num != 0)
  27.     {
  28.         n = n + 1;
  29.         if (n == 1)
  30.             head = p;/*------------第一次时赋值给head---------------*/

  31.         p = p->next;/*------------以后每次指向自身的下个节点---------------*/

  32.         p = (Stu*)malloc(LEN);     //开辟动态存储区,把起始地址赋给p1
  33.         if (!p)
  34.         {
  35.             printf_s("内存申请失败\n");
  36.             return head;
  37.         }

  38.         scanf_s("%ld%*c%f", &p->num, &p->score);   //输入其他学生的学号和成绩
  39.     }
  40.    
  41.     return head;
  42. }
  43. int main()
  44. {
  45.     Stu* pt;
  46.     pt = creat();  //函数返回链表第一个结点的地址
  47.     printf("\n num:%ld\n score:%5.1f\n", pt->num, pt->score);  //输出第一个结点的成员值
  48.     return 0;
  49. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 22:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表