33,311
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
struct AAA{
int i;
int j;
};
struct AAA g_BBB[8];
int main()
{
std::cout <<"1 : g_BBB[3].i ==> "<< g_BBB[3].i <<std::endl;
std::cout <<"1 : &g_BBB[3].i ==> "<< &g_BBB[3].i <<std::endl;
int i = 0;
for (i=0; i<8; i++)
{
g_BBB[i] = (struct AAA){i, i};
}
std::cout <<"2 : g_BBB[3].i ==> "<< g_BBB[3].i <<std::endl;
std::cout <<"2 : &g_BBB[3].i ==> "<< &g_BBB[3].i <<std::endl;
return 0;
}
1 : g_BBB[3].i ==> 0
1 : &g_BBB[3].i ==> 0x8049c98
2 : g_BBB[3].i ==> 3
2 : &g_BBB[3].i ==> 0x8049c98
g_BBB[i] = (struct AAA){i, i};
估计lz有一点还没有明白:在c++里,(struct AAA){i, i}只是一个临时构造成你执行的类型的变量
struct AAA temp = {i, i};
g_BBB[i]=temp;