참조자와 포인터
reference, pointer
실습 예제
ch04ex1_swap.cpp
// 두 값을 변경하는 함수 swap
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
void swap(int& x, int& y);
void swap(int* p_x, int* p_y);
int main()
{
int n1 = 5;
int n2 = 7;
cout << "origin n1: " << n1 << ", n2: " << n2 << endl;
swap(n1, n2);
cout << "swap reference n1: " << n1 << ", n2: " << n2 << endl;
swap(&n1, &n2);
cout << "swap pointer n1: " << n1 << ", n2: " << n2 << endl;
return 0;
}
void swap(int& x, int& y)
{
int t;
t = x;
x = y;
y = t;
}
void swap(int* p_x, int* p_y)
{
int t;
t = *p_x;
*p_x = *p_y;
*p_y = t;
}실습 문제
ch04lab1_reference.cpp
두 수(정수 i, 실수 d)를 입력 받는 input()함수를 참조자(reference)로 구현하시오.
ch04lab2_pointer.cpp
두 수(정수 i, 실수 d)를 입력 받는 input()함수를 포인터(pointer)로 구현하시오.
Last updated