構造体を使った配列へのアクセス
code:c
#include <stdio.h>
#define SETNUM 3
//構造体
typedef struct {
int test1;
int test2;
int test3;
} Gtest;
//staticを使用し、起動時からファイル内グローバル変数。メイン関数に入る前に、呼ばれる。
static Gtest tesSETNUM = {
{ 0, 0, 0}, // 0
{ 1, 2, 3}, // 1
{ 4, 5, 6}, // 2
};
int main(void){
// Your code here!
for(int i=0;i<SETNUM;i++){
Gtest *ptest = &(tesi);
printf("ptest=%d\n",ptest->test1);
printf("ptest=%d\n",ptest->test2);
printf("ptest=%d\n",ptest->test3);
}
}
https://paiza.io/projects/SCB36J1qz0wvZWXbVYhhhQ
code:c
#include <stdio.h>
#define SETNUM 3
//構造体
typedef struct {
int test1;
int test2;
int test3;
} Gtest;
//staticを使用し、起動時からファイル内グローバル変数。メイン関数に入る前に、呼ばれる。
// Gtest構造体の配列を1つ用意し、test1, test2, test3を0で初期化
#if 0
static Gtest tes1 = {
{0, 0, 0} // 1つのGtest構造体を全て0で初期化
};
#else
static Gtest tes3={0,1,2};
#endif
void TesSet(Gtest *ptest);
int main(void){
Gtest *ptest = &(tes0);
printf("ptest=%d\n",ptest->test1);
printf("ptest=%d\n",ptest->test2);
printf("ptest=%d\n",ptest->test3);
TesSet(ptest);
printf("ptest=%d\n",ptest->test1);
printf("ptest=%d\n",ptest->test2);
printf("ptest=%d\n",ptest->test3);
}
void TesSet(Gtest *ptest){
ptest->test1 = 2;
}
https://paiza.io/projects/M08SK94rw-fcaK4mThCI8w?language=c
https://paiza.io/projects/GSfQ-YFjYVFHwUOw4Hw7Cg?language=c
https://paiza.io/projects/QxX7l0_r2Jg30ww47FrHLg?language=c