포인터 연산

이승덱·2021년 7월 21일

CPP

목록 보기
16/70
#include <iostream>

using namespace std;

//포인터 연산

//주소 연산자 &

//산술 연산자 (+, -)

//간접 연산자

//간접 멤버 연산자

struct  Player

{

 int hp;

 int damage;

};

int main()

{

 int number = 1;

 //주소 연산자 &

 //해당 변수의 주소를 알려줌

 //정확히는 해당 변수 TYPE에 따라서 TYPE* 로 변환

 int* ptr = &number;

 //산술 연산자 + / - / ++ / --

 //포인터 타입의 산술연산은 +(다음 주소) -(이전 주소)로의 이동을 의미함

 //즉 +1은 1을 더하라는 말이 아닌 1*(type 크기)를 이동하라는 뜻임

 //ptr = ptr + 1;

 //ptr++;

 //++ptr;

 //ptr += 1;

 //간접 연산자 *

 //- 해당 주소로 이동

 * ptr = 3;

 Player player;

 player.hp = 100;

 player.damage = 100;

 Player* playerInfo;

 (*playerInfo).damage = 10;

 (*playerInfo).hp = 200;

 //간접 멤버 연산자 ->

 //*과 .을 합친 의미

 playerInfo->hp = 300; // = (*playerInfo).hp = 300; 과 같은 의미

 playerInfo->damage = 20;

 return 0;

}
profile
공부 기록용 블로그입니다

0개의 댓글