首页 > 技术文章 > C++之数据、结构体初始化值为0

FKdelphi 2021-04-02 10:53 原文

 

 

 

 1 void MainWindow::on_pushButton_clicked()
 2 {
 3      int nInt[20];
 4      nInt[0] = 1;
 5      nInt[1] = 2;// 数据乱的一塌糊涂
 6 
 7      int nInt2[20];
 8      memset(nInt2, 0, sizeof(nInt2));
 9      nInt2[0] = 1;
10      nInt2[1] = 2;// 数据全是0,整齐
11 
12      struct ConTest
13      {
14          int nName;
15          int aNames[20];
16      };
17 
18      ConTest oConTest; // ConTest oConTest = {0}; 在VS中好使,在QT中不好使。
19      oConTest.aNames[0] = 1;
20      oConTest.aNames[1] = 2;// nName、aNames里面乱的不成样子。
21 
22      ConTest oConTest2; // ConTest oConTest = {0}; 在VS中好使,在QT中不好使。
23      memset(&oConTest2, 0, sizeof(ConTest));
24      oConTest2.aNames[0] = 1;
25      oConTest2.aNames[1] = 2;// 全部都很整齐,全部为0
26 }

 

推荐阅读