struct中数组成员在外部无法赋值

#include <iostream>
#include<cstring>


int main()
{
    using namespace std;

    struct computer
    {
        char brand[20];
        float price;
    }com1 = { "Dell",5000 }, com2;
    cout << "电脑品牌" << com1.brand << ",价格:" << com1.price << endl;
    
// com2.brand = "hello";  错误,数组成员只有在初始化时,才能对数组整体进行赋值。
// com2.brand[] = 'a'; 正确,只能数组某一个下标赋值。
strcpy(com2.brand, "hello"); //想对数组整体赋值,就要用strcpy之类的函数进行操作 com2.price = 33.3; return 0; }

  

猜你喜欢

转载自www.cnblogs.com/Open-Source/p/12919990.html
今日推荐