的代碼:定義結構陣列中的功能
int i;
struct st
{
int m;
}st_t[2];
void foo()
{
i = 4;
st_t[2] =
{
{10},{20}
}; // it's wrong !!!! but I don't know how to do.
}
int main()
{
foo();
cout<<i<<endl; // will output 4;
cout<<st_t[0].m<<endl; // this should output 10
cout<<st_t[1].m<<endl; // this should output 20
return 0;
}
是否有可能定義的功能的結構體數組?如果是,那該怎麼做? 在此先感謝。
PS:
對不起,我的英文不好。我正在製作一個Tetris遊戲,它有一個Shape類,我在Shape.h中聲明瞭一個shape結構數組,然後在Shape.cpp的Shape構造函數中分配給了struct數組。這樣對嗎?或如何分配給結構數組,所以我可以在另一個函數中使用它?
從代碼看來,您似乎想要分配給全局數組,而不是在函數中定義數組。你究竟想要做什麼? –