• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

博客园    首页    新随笔       管理    订阅  订阅

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; }

  

posted @ 2020-05-19 22:19  ヤ玥夜ゞ  阅读(218)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3