wangrui2000_wr 2019-12-28 22:17 采纳率: 0%
浏览 1228
已结题

结构体指针成员变量在赋值后被篡改了是怎么回事?

结构体指针地址没变,值被篡改了什么情况,有没有大神知道啊
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "iostream"
#include "string.h"
using namespace std;

struct STU_LIST
{
char name[40];
int num;
char sex[4];
int clas;
struct STU_LIST nex;
};
/
*****************************************************************************************************************/
/*------------------------------------ -----数字提取------------ ---------------------------------------------*/
/******************************************************************************************************************/

int strEint(const char str)
{
int len = strlen(str),i,num;
char *tepnum,*tep;
tepnum = new char[40];
tep=tepnum;
i=len-1;

*(tepnum+len) = '\0';
while (i>=0)
{
if (
(str+i)>='0'&&*(str+i)<='9')
{
(tepnum+i)=(str+i);
}
else
{

tepnum=tepnum+i+1;
i=0;
}
i--;
}
num = atoi(tepnum);
delete[] tep;
return num;
}

struct STU_LIST STU_insirt(struct STU_LIST *head,int n);
struct STU_LIST *PRESENT_class;
/
*****************************************************************************************************************/
/*------------------------------------ -----主程序------------ -------------------------------------------------*/
/******************************************************************************************************************/

int _tmain(int argc, _TCHAR* argv[])
{
char *comad;
int lep = -1;
bool state = true,create_sign = false;
comad = new char;
cout<<"欢迎使用学生名单录入工具,请输入命令命令进行操作。\n输入help查询可用命令。"< PRESENT_class = new struct STU_LIST;
PRESENT_class->clas = 10;
PRESENT_class->num = 0;
PRESENT_class->nex = NULL;
while (state)
{
cin>>comad;
int nums;
struct STU_LIST *temp;
nums = strEint(comad);
if (nums <= 0)
{
cout<<"您的操作命令缺少有效参数或参数类型有误!!!"< cout }
else
{
if(PRESENT_class != NULL)
{
temp = STU_insirt(PRESENT_class,nums);
coutnex->"<(temp->nex)< coutnex->num->"<(&temp->nex->num)< coutclas<<"班"<name<<"的信息记录已添加。"< if (temp->nex == NULL)
{
cout<<"本节点是尾结点"< }
else
{
coutnum< coutnex->num<<endl;
}
}
else
{
cout<<"您要往哪个班级添加学生信息,请在添加信息前先选择一个班级的登记表。"<<endl;
cout<<"您可以输入help来查询可用操作命令,及使用格式和方法。"<<endl;
}
}
if (strstr(comad,"exit")!=NULL)
{
state = false;
}
}
return 0;
}

/******************************************************************************************************************/
/*-----------------------------------------插入学生条目-----------------------------------------------------------*/
/******************************************************************************************************************/
struct STU_LIST STU_insirt(struct STU_LIST *head,int n)
{
struct STU_LIST *temp,*stp1,*lastsp;
temp = head;
lastsp = NULL;
bool sign = true;
while (sign)
{
cout<<"当前节点学号是"<num< if(temp->num >= n) //假如要插入的学号不大于当前节点学号
{
if(temp->num > n ) //假如要插入的学号小于当前节点学号,那么在当前节点前面创建新节点并录入学生数据
{
stp1=(struct STU_LIST
) alloca(sizeof(struct STU_LIST));

stp1->num = n;
stp1->clas = head->clas;
cout<<"请输入学生姓名:";
cin >>stp1->name;

cout<<"请输入学生性别:";
cin >>stp1->sex;

stp1->nex = temp;
if(temp == head ) //假如当前节点是头结点,那么设置新节点为表头
{
head = stp1;
sign = false;
}
else //假如当前节点不是头结点,将上一个节点的指针指向新节点,跳出循环结束命令
{
lastsp->nex = stp1;
sign = false;
}

        }
        else                                                                                    //假如要插入的学号等于当前节点学号,那么更新该节点学生数据。
        {
            cout<<"请输入学生姓名:";
            cin >>temp->name;                                                               
            cout<<"请输入学生性别:";
            cin >>temp->sex;                                                                
            sign = false;
        }
    }
    else                                                                                    //假如要插入的学号大于当前节点学号
    {
        cout<<"要插入的节点大于当前节点"<<endl;
        if(temp->num == 0)                                                                      //假如当前节点学号等于0,意味着当前节点没有录入过数据,那么将本节点作为插入的节点录入学生数据。
        {
            temp->num = n;
            cout<<"请输入学生姓名:";
            cin >>temp->name;
            cout<<"请输入学生性别:";
            cin >>temp->sex;
            temp->nex = NULL;
            sign = false;
        }
        else                                                                                    //假如当前节点的学号不等于0。
        {
            if(temp->nex == NULL)                                                                   //假如当前节点是尾结点,那么在当前节点之后创建新节点
            {
                stp1 = (struct STU_LIST*) alloca(sizeof(struct STU_LIST));
                cout<<"stp1 -> "<<static_cast<const void *>(stp1)<<endl;
                stp1->num = n;
                cout<<"新节点的学号是"<<stp1->num<<endl;
                stp1->clas = head->clas;
                stp1->nex = NULL;
                cout<<"请输入学生姓名:";
                cin >>stp1->name;
                cout<<"请输入学生性别:";
                cin >>stp1->sex;
                temp->nex = stp1;
                cout<<"temp->nex->num="<<temp->nex->num<<endl;
                cout<<"temp->nex->num->"<<static_cast<const void *>(&temp->nex->num)<<endl;
                sign = false;
            }
            else                                                                                    //假如当前节点不是尾结点,那么将当前节点设置为下个节点
            {
                lastsp = temp;
                temp = temp->nex;
            }
        }
    }
}
if(head->nex != NULL)
{
cout<<"head->nex->num="<<head->nex->num<<endl;
cout<<"head->nex->num->"<<static_cast<const void *>(&head->nex->num)<<endl;
}
return head;

}
图片说明
在函数内部时候值不变,一出函数内存里的值就变了这是什么道理。

  • 写回答

2条回答 默认 最新

  • threenewbee 2019-12-29 10:50
    关注

    指针作为参数在函数内修改,要作用到调用者,必须使用双指针(C)或者引用(C++)
    否则显然不能修改。和
    void foo(int i)
    {
    i = 2;
    }
    main()
    {
    int i;
    foo(i);
    此时i显然还是原来的状态,不是2
    }
    是一个道理

    评论

报告相同问题?

悬赏问题

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