struct定义好结构时,就必须赋初始值吗?
本帖最后由 〃忝書γě渎ぐ 于 2020-5-19 20:53 编辑#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
struct computer
{
char brand;
float price;
}com1 = {"Dell",5000},com2;
cout<<"电脑品牌"<<com1.brand<<",价格:"<<com1.price<<endl;
// com2.brand = "hello"; 为什么报错?
com2.price = 33.3; //为什么这行就成功
return 0;
}
改成#include <iostream>
#include<cstring>
int main()
{
using namespace std;
struct computer
{
char brand;
float price;
}com1 = { "Dell",5000 }, com2;
cout << "电脑品牌" << com1.brand << ",价格:" << com1.price << endl;
strcpy(com2.brand, "hello"); //为什么报错?
com2.price = 33.3; //为什么这行就成功
return 0;
}字符数组只有初始化时才能赋值,其他时候得用 strcpy。 https://www.cnblogs.com/losesea/archive/2012/11/15/2772526.html 永恒的蓝色梦想 发表于 2020-5-19 21:41
改成字符数组只有初始化时才能赋值,其他时候得用 strcpy。
万分感谢!
页:
[1]