배열 기초

namu·2022년 7월 6일
// 배열의 크기는 상수여야 함 (컴파일러에 따라 다르며, VC 컴파일러 기준)
const int monsterCount = 10;
StatInfo monsters[monsterCount];

StatInfo* monster_0 = monsters;
monster_0->hp = 100;
monster_0->attack = 10;
monster_0->defence = 1;

StatInfo* monster_1 = monsters + 1;
monster_1->hp = 200;
monster_1->attack = 20;
monster_1->defence = 2;

StatInfo& monster_2 = *(monsters + 2);
monster_2.hp = 300;
monster_2.attack = 30;
monster_2.defence = 3;

StatInfo temp;
temp = *(monsters + 2);

for (int i = 0; i <10; i++)
{
	StatInfo& monster = *(monsters + i);
    monster.hp = 100 * (i + 1);
    monster.attack = 10 * (i + 1);
    monster.defence = i + 1;
}

인덱스 사용법

for (int i = 0; i <10; i++)
{
    monsters[i].hp = 100 * (i + 1);
    monsters[i].attack = 10 * (i + 1);
    monsters[i].defence = i + 1;
}

배열 초기화 문법

	int numbers[5]; // 디버그 모드에서 스택은 초기에 cc라는 기본값으로 설정됨.
	int numbers[5] = {}; // 기본값인 0으로 초기화됨.
xor eax,eax
mov dword ptr [numbers],eax
mov dword ptr [ebp-124h],eax
mov dword ptr [ebp-120h],eax
mov dword ptr [ebp-11Ch],eax
mov dword ptr [ebp-118h],eax
int numbers1[10] = { 1, 2, 3, 4, 5 };
mov dword ptr [numbers1],1
mov dword ptr [ebp-154h],2
mov dword ptr [ebp-150h],3
mov dword ptr [ebp-14Ch],4
mov dword ptr [ebp-148h],5
xor eax,eax
mov dword ptr [ebp-144h],eax
mov dword ptr [ebp-140h],eax
mov dword ptr [ebp-13Ch],eax
mov dword ptr [ebp-138h],eax
mov dword ptr [ebp-134h],eax
int numbers2[] = { 1,2,3,4,11,24,124,11,1 }; // 데이터 개수만큼의 크기에 해당하는 배열로 만들어준다.
char helloStr[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
profile
안녕하세요

0개의 댓글