Forever_lll231 2023-03-09 06:34 采纳率: 100%
浏览 38
已结题

c++编程时遇到的小问题


#include <iostream>
#include <string>
using namespace std;
struct Student{
    string name;
    int score;
    struct Student *next;
};
int menu()
{
    int choice;
    do
    {
        cout<<"请输入您的选择:"<<endl;
        cout<<"  1.成绩添加"<<endl;
        cout<<"  2.成绩删除"<<endl;
        cout<<"  3.成绩排序"<<endl;
        cout<<"  4.成绩插入"<<endl;
        cin>>choice;
        system("cls");
    }while(choice!=1 && choice!=2 && choice!=3 && choice!=4);
    return choice;
}
struct Student *addScore(struct Student *head,struct Student *latest_node)
{
    int score;
    if(head==NULL){
        Student *head=new Student;
        latest_node=head;
        cout<<head<<endl;
    }
    do
    {
        cout<<head<<endl;
        Student *p=new Student;
        cout<<"请输入姓名:";
        cin>>p->name;
        cout<<"请输入分数(输入负数时停止输入):";
        cin>>p->score;
        score=p->score;
        //system("cls");
        latest_node->next=p;
        //cout<<latest_node<<endl;
        //cout<<latest_node->next<<endl;
        
        latest_node=p;
        p->next==NULL;
    }while(score>=0);
    cout<<"测试"<<endl;
    cout<<&head<<" "<<head->next;
    return latest_node;
}
int main()
{
    Student *head = NULL;
    Student *latest_node = NULL;
    int choice = menu();
    switch(choice)
    {
        case 1:{
            latest_node=addScore(head,latest_node);
            break;
        }
        case 2:{
            break;
        }
        case 3:{
            break;
        }
        case 4:{
            break;
        }
    }
    return 0;
 } 

为什么代码31行和35行的代码运行结果不一样?
head地址为什么在35行变成了0?

  • 写回答

3条回答 默认 最新

  • pureblackman 2023-03-09 08:44
    关注

    img


    你目的使用的head一直没有被初始化空间,29行的head 是你新定义的局部变量

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

报告相同问题?

问题事件

  • 系统已结题 3月17日
  • 已采纳回答 3月9日
  • 修改了问题 3月9日
  • 修改了问题 3月9日
  • 展开全部

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改