在2个不同的结构中分配2个字符串:: Array

问题描述 投票:0回答:1

这是第一个结构并包含第一个成员

#include <iostream>
#include <algorithm>
#include <cstring>
struct Student_Data {
    string Assignment[200];
};

这是第二个结构,包含一个成员

struct Courseassign { 
    string createassig[200];
};

//here is declare function 
Doc(Student_Data ,Courseassign );
//the main function to declare 2 structures and call the Doc() function 
int main()
{
    Courseassign phase1 ; // declare 1st struct 
    Student_Data Student_Details ; // declare 1st struct 
    Doc(  phase1  , Student_Details); // Call Doc() function to run it
}

这是我们的功能,我已经尝试使用copy(),但它也没有工作,我知道cpy应该是char而不是字符串所以我只是想使成员等于另一个结构中的另一个成员使用字符串排列

void Doc(Courseassign phase1, Student_Data Student_Details )
{
    for(int i=0;i<200;i++) {
        cout<< "create assign " << endl ; 
        cin >> phase1.createassig[i]  ; // here to enter member value 
        // here it's just a try to make them equal each other but it failed
        Student_Details.Assignment[i] = phase1.createassig[i];
        // here it should be Array of char but i need to Array of strings so what should i use
        cpy( Student_Details.Assignment[i],phase1.createassig[i]); 
    }
}
c++ arrays string struct
1个回答
0
投票

当你尝试时会得到什么错误:Student_Details.Assignment[i] = phase1.createassig[i];

你提到的strcpy是针对char*s的,如果你想将一个字符串值赋给另一个字符串变量,你也可以使用string::assign

例如,在您的情况下,它将是:

Student_Details.Assignment[i].assign( phase1.createassig[i] );

最后,如@Peter的评论中所述,您将按值传递结构,因此您在其中所做的任何更改都不会传播到传递给函数的实际变量。将在本地对它们进行更改,并在函数终止时销毁。您可以尝试通过引用传递结构,即

void Doc(Courseassign&, Student_Data&);
© www.soinside.com 2019 - 2024. All rights reserved.