关于c:CC-关于结构体变量传参时遇到的问题

50次阅读

共计 2439 个字符,预计需要花费 7 分钟才能阅读完成。

论述

主函数中的构造体变量作为非主函数的参数时, 若只是将构造体变量名传与非主函数, 那就意味着仅是将其值传递给了非主函数外部, 相当于在非主函数外部复制粘贴了一个新的构造体 (此处称之为 X), 这个新构造体 X 与主函数中的构造体(此处称之为 Y) 是互相独立的, 但它们二者的初始成员值都雷同.

struct SSS{...};

void NonMain(SSS X){
    // X & Y 的初始成员值都雷同, 地址却相异
    ...
}

int mian(){
    SSS Y;
    NonMain(Y);
    return 0;
}

根据上述代码, 如果在 NonMain 函数中扭转了构造体变量外部的值, 当 NonMain 函数运行完结后, 程序返回主函数时, 主函数中原有的构造体变量外部不会有丝毫变动.
这是因为构造体变量名自身不是地址, 非主函数内所操作的构造体变量的地址, 并非主函数中的构造体变量的地址.

因而, 正确的构造体变量传参加非主函数时, 须要先取出构造体变量的地址当作指针, 再将指针传与非主函数.

代码示例

#include <string>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

typedef struct TimesRecord
{
        int year;
        int month;
        int day;
} times;

void initializationError(TimesRecord date_record)
{
        date_record.year = 2015;
        date_record.month = 5;
        date_record.day = 18;

        cout << "initializationError.&date_record:" << &date_record << endl;
}

void alterError(TimesRecord date_record, int newYear, int newMonth, int newDay)
{
        date_record.year = newYear;
        date_record.month = newMonth;
        date_record.day = newDay;
        cout << "alterError.&date_record:" << &date_record << endl;
}

/* ----------------------------------------------------------------------------- */

void initializationCorrect(TimesRecord &date_record)
{
        date_record.year = 2015;
        date_record.month = 5;
        date_record.day = 18;
        cout << "initializationCorrect.&date_record:" << &date_record << endl;
}

void alterCorrect(TimesRecord &date_record, int newYear, int newMonth, int newDay)
{
        date_record.year = newYear;
        date_record.month = newMonth;
        date_record.day = newDay;
        cout << "alterCorrect.&date_record:" << &date_record << endl;
}

int main(int argc, char const *argv[])
{
        TimesRecord t;
        int newYear = 2006;
        int newMonth = 9;
        int newDay = 22;

        initializationError(t);
        cout << "Error initialization:";
        cout << t.year << "-" << t.month << "-" << t.day << endl;
        cout << "main.&t:" << &t << endl;

        cout << endl;

        alterError(t, newYear, newMonth, newDay);
        cout << "Error Alter:";
        cout << t.year << "-" << t.month << "-" << t.day << endl;
        cout << "main.&t:" << &t << endl;

        cout << endl;

        initializationCorrect(t);
        cout << "Correct initialization:";
        cout << t.year << "-" << t.month << "-" << t.day << endl;
        cout << "main.&t:" << &t << endl;

        cout << endl;

        alterCorrect(t, newYear, newMonth, newDay);
        cout << "Correct Alter:";
        cout << t.year << "-" << t.month << "-" << t.day << endl;
        cout << "main.&t:" << &t << endl;

        return 0;
}


运行后果

initializationError.&date_record: 0x7ffc498004b0
Error initialization: -5234528-22046-1233126896
main.&t: 0x7ffc498004e8

alterError.&date_record: 0x7ffc498004b0
Error Alter: -5234528-22046-1233126896
main.&t: 0x7ffc498004e8

initializationCorrect.&date_record: 0x7ffc498004e8
Correct initialization: 2015-5-18
main.&t: 0x7ffc498004e8

alterCorrect.&date_record: 0x7ffc498004e8
Correct Alter: 2006-9-22
main.&t: 0x7ffc498004e8

正文完
 0