포인터 (Pointer)
포인터란 우리가 사용하는 변수가 실제로 담기는 주소를 의미한다! 우리가 만드는 변수는 분명 컴퓨터 어딘가 잘 보관이 될 테고, C++에서 우리는 그 주소를 확인해 볼 수 있당
※ c++에만 있는 것 ( 다른 언어에는 없는 기능 )
& = 어떤 변수의 주소를 가져와라.
// pointer.cpp
#include <iostream>
int main()
{
using namespace std;
int watermelons = 10;
int *p_watermelonBox = &watermelons; // int * : 난 포인터 함수를 선언, 그리고 주소 넣기
cout << "watermelons: " << watermelons << endl;
cout << "watermelons in p_watermelonBox = " << *p_watermelonBox << endl;
cout << "watermelons location: " << &watermelons << endl; //주소
cout << "p_watermelonBox value: " << p_watermelonBox << endl; //pointer, 주소랑 pointer랑 같아야함
*p_watermelonBox = *p_watermelonBox + 1; //수박 주소에 하나더하기
cout << "Now I have " << watermelons << endl; //수박 출력
}
< 중요 : 헷갈릴 수 있음 >
int watermelons = 10;
int *p_watermelonBox = &watermelons;
- 선언과 동시에 초기화: 포인터 변수 p_watermelonBox를 선언하면서 &watermelons로 바로 초기화합니다.
- 안전성: 포인터가 생성될 때부터 유효한 주소를 가리키므로, 초기화 누락으로 인한 오류를 방지할 수 있습니다.
int watermelons = 10;
int *p_watermelonBox;
p_watermelonBox = &watermelons;
- 선언 후 나중에 초기화: 먼저 포인터 변수 p_watermelonBox를 선언하고, 다음 줄에서 &watermelons로 초기화합니다.
- 주의점: 포인터를 초기화하기 전에 사용하게 되면 예기치 않은 동작이나 오류가 발생할 수 있습니다.
< 결과 값 >

주소랑 포인터의 값이 일치해야 하는게 중요
포인터는 C/C++을 강력하게 만들어주기도 하지만 자칫 메모리 누수(memory leak)가 발생할수 있음
참조(reference)
참조는 별명이라고 생각하면 됨.
약간 핑코는 어디에 있어 = 고양이는 어디에 있어
// reference.cpp
#include <iostream>
int main()
{
using namespace std;
int numOfWatermelons = 10;
int &numOfMyFavouriteFruits = numOfWatermelons; //내가 좋아하는과일 = 수박
numOfMyFavouriteFruits++; //내가 좋아하는 과일 증가시킴
cout << numOfWatermelons << endl; //수박 몇개 있어?
cout << &numOfWatermelons << endl; //수박 어디에 있어?
cout << &numOfMyFavouriteFruits << endl; //내가 좋아하는 과일 어디에 있어?
}

여기서 주소가 같다는 것을 확인할 수 있음
call by value, call by reference
printMessage 는 두 정수를 받아 값을 출력하는 함수
callByValue 는 두 정수를 값으로 전달받아(복사본) 작업하는 함수입니다.
함수 내에서 값을 바꿔도 원본 변수에는 영향을 주지 않습니다.
callByReference는 두 정수를 참조로 전달받아(원본에 직접 접근) 작업하는 함수입니다.
함수 내에서 값을 바꾸면 원본 변수의 값도 바뀝니다.
//callbyreferenceandvalue.cpp
#include <iostream>
using namespace std;
void printMessage(int a, int b);
void callByValue(int a, int b);
void callByReference(int &a, int &b);
int main()
{
int a = 10;
int b = 20;
cout << "&a: " << &a << endl;
cout << "&b: " << &b << endl;
cout << "callByValue" << endl;
printMessage(a, b);
callByValue(a, b);
printMessage(a, b);
cout << "callByRefernece" << endl;
printMessage(a, b);
callByReference(a, b);
printMessage(a, b);
return 0;
}
void printMessage(int a, int b)
{
cout << "------------" << endl;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
cout << "------------" << endl;
}
void callByValue(int a, int b)
{
cout << "IN callByValue" << endl;
cout << "&a: " << &a << endl;
cout << "&b: " << &b << endl; // 주소찍고
int tmp = a; // 값 바꾸기
a = b;
b = tmp;
}
void callByReference(int &a, int &b) // 참조로 별명을 만들어 가져옴
{
cout << "IN callByReference" << endl;
cout << "&a: " << &a << endl;
cout << "&b: " << &b << endl;
int tmp = a;
a = b;
b = tmp;
}


'개인 개발 공부 > C ++' 카테고리의 다른 글
| 1-8. 배열과 2차원 배열 (0) | 2024.11.11 |
|---|---|
| 1-6 함수 (1) | 2024.11.10 |
| 1-5 반복문과 다중반복문 (0) | 2024.11.09 |
| 조건문 (0) | 2024.11.09 |
| 연산자 (0) | 2024.11.09 |