회원 로그인
|
포인터
C++ 포인터
- 일반 변수의 주소값를 저정하는 변수
- 메모리 공유 참조
[포인터 변수 문법]
1. 생성
자료형* 포인터변수;
2. 일반변수의 주소
&변수;
3. 포인터 변수에 주소 대입
포인터변수 = &일반변수
[레퍼런스 변수]
- 일반 변수의 별칭으로 일반변수를 접근하는 두개의 이름이 부여됨
[레퍼런스 변수 문법]
1. 생성
자료형& 레퍼런스변수 = 일반변수
[기억클래스]
- 자동 변수 : 함수의 선언된 매개변수로 함수 호출시 생성하고 호출이 종료될때 자동 소멸함
- 지역변수 : 블럭안에서 선언된 변수로 사용범위가 블록 내부로 한정됨
- 전역 변수 : 생략 (설명만)
- 정적 변수 : 클래스 설명 부분에서 설명함
- 클래스 멤버 변수 : 클래스 설명 부분에서 설명함
#include <iostream> // cin, cout #include <string> // string #include <stdio.h> // rand() #include <time.h> // time() #include <Windows.h> // Sleep() using namespace std; // 매개변수가 있는 함수 사용시 변수가 스왑 안됨 void Swap1(int a, int b) { int t = 0; // a와 b값을 교체함 t = a; a = b; b = t; } // 포인터를 사용하여 변수 스왑 void Swap2(int* a, int* b) { int t = 0; // a와 b값을 교체함 t = *a; *a = *b; *b = t; } void main() { int a = 10; int b = 20; cout << "a : " << a << ", b : " << b << endl; Swap1(a, b); cout << "a : " << a << ", b : " << b << endl; cout << endl; // 포인터 Swap2(&a, &b); cout << "a : " << a << ", b : " << b << endl; }
#include <iostream> // cin, cout #include <string> // string #include <stdio.h> // rand() #include <time.h> // time() #include <Windows.h> // Sleep() using namespace std; class Monster2 { public: int hp; int damage; }; void Swap(int* a, int* b) { int t = 0; t = *a; *a = *b; *b = t; } void main() { int iValue = 10; float fValue = 10.2; // char cValue = 'A'; Monster2 monster2; monster2.hp = 100; monster2.damage = 10; // & : 주소 연산자 // 메모리의 주소는 16진수로 8자리로 표현/시작 cout << "&iValue : " << &iValue << endl; cout << "&fValue : " << &fValue << endl; cout << "&monster2 : " << &monster2 << endl; // char형 주소 // cout << "&cValue : " << &cValue << endl; // C++ // printf("&cValue %\n", &cValue); // C cout << endl; // * : 주소연산자 앞에 *를 붙이면 주소의 값을 알 수 있음 cout << "*(&iValue) : " << *(&iValue) << endl; // value = *(&value) cout << "*(&iValue) : " << *(&(*(&(*(&iValue))))) << endl; cout << "*(&fValue) : " << *(&fValue) << endl; cout << "(*&monster2).hp : " << (*&monster2).hp << endl; cout << endl; // 자료형* : 특정 변수의 주소를 저장할 수 있는 타입 // 자료형은 반드시 주소를 저장하려는 변수의 타입과 일치해야 함 (같은 형 주소만 저장할 수 있는 타입) // 위 주소연산자 *와 다르다. int* piValue = &iValue; // cout << "piValue) : " << piValue << endl; // int형 주소만 cout << "*piValue) : " << *piValue << endl; Monster2* pMonster2 = &monster2; cout << "pMonster : " << pMonster2 << endl; cout << "&pMonster : " << &pMonster2 << endl; cout << "(*(&pMonster)).hp : " << (*(&pMonster2)).hp << endl; // (monster).hp cout << "(*pMonster).hp : " << (*pMonster2).hp << endl; cout << "pMonster->hp : " << pMonster2->hp << endl; // -> 어록 //iValue = int형 변수 //&Value = & 주소 //pValue = * 주소 값 읽음 }
/* 몬스터 간의 hp, damage 스왑 */ #include <iostream> // cin, cout #include <string> // string #include <stdio.h> // rand() #include <time.h> // time() #include <Windows.h> // Sleep() using namespace std; class Monster3 { public: int hp; int damage; }; void objectSwap(Monster3* m1, Monster3* m2){ Monster3 t; t = *m1; *m1 = *m2; *m2 = t; } // hp만 스왑 시 void MonsterHpChange(Monster3* m1, Monster3* m2){ Monster3 t; t = m1->hp; m1->hp = m2->hp; m2->hp = t; } void main() { Monster3 monster1; monster1.hp = 100; monster1.damage = 10; Monster3 monster2; monster2.hp = 50; monster2.damage = 20; objectSwap(&monster1, &monster2); cout << "monster1.hp >> " << monster1.hp << endl; cout << "monster2.hp >> " << monster2.hp << endl; }
#include <ostream> // cin, cout #include <string> // string #include <stdio.h> // rand() #include <time.h> // time() #include <Windows.h> // Sleep() using namespace std; void Input(int* speed, int* hp, int* damage) { // 2.주소로 받고 cout << "input speed : "; cin >> *speed; // 3.주소에 실체를 넣고 cout << "input hp : "; cin >> *hp; cout << "input damage : "; cin >> *damage; } void Output(int speed, int hp, int damage) { // 5.실체를 출력 cout << "output speed : " << speed << endl; cout << "output hp : " << hp << endl; cout << "output damage : " << damage << endl; } void main() { int speed = 0; int hp = 0; int damage = 0; Input(&speed, &hp, &damage); // 1.주소로 넣고 cout << endl; Output(speed, hp, damage); // 4.실체를 넣고 }
/* 포인터 메모리의 참조 */ #include <iostream> // cin, cout #include <string> // string #include <stdio.h> // rand() #include <time.h> // time() #include <Windows.h> // Sleep() using namespace std; class Monster5 { public: int speed; int hp; int damage; }; Monster5 InputMonster2() { Monster5* monster; cout << "Monster speed : "; cin >> monster->speed; cout << "Monster hp : "; cin >> monster->hp; cout << "Monster damage : "; cin >> monster->damage; return monster; } void InputMonster(Monster5* monster) { // 2. 객체 주소로 받고 cout << "Monster speed : "; cin >> monster->speed; // 3.객체의 맴버변수에 주소로 접근하여 값을 입력(대입)하고 (어록) cout << "Monster hp : "; cin >> monster->hp; cout << "Monster damage : "; cin >> monster->damage; cout << "Monster 객체 변수의 크기 : " << sizeof(monster) << endl; // 참조받은 메모리는 기본 메모리 보다 적다 } void OutputMonster(Monster5 monster) { cout << "Monster speed : " << monster.speed << endl; // 5.객체의 맴버변수의 값을 출력 monster.hp = 100; // 이라할 때 이 값이 들어가는 것이 아닌 주소에 들어간 값이 출력된다. // 이유는 이 함수 안에서만 처리하기 때문에 주소값에 영향을 주지 않아 명령이 끝나면 소멸된다. cout << "Monster hp : " << monster.hp << endl; cout << "Monster damage : " << monster.damage << endl; } void main() { Monster5 monster; cout << "monster 객체 변수의 크기 : " << sizeof(monster) << endl; // 기본 메모리 InputMonster(&monster); // 1.객체의 주소를 넘기고 monster = InputMonster2(); // 이렇게 리턴으로 입력 받아올 수 있음 cout << endl; OutputMonster(monster); // 4.객체의 맴버변수를 넣고 }
[포인터와 배열의 관계]
*배열의 구조는 주소를 가진 포인터 상수
*배열의 메모리/포인터 쓰임새
배열에는 모두 같다
pArr[] = arr[]
*(arr + 1) == *(pArr + 1) == pArr[1]
int* pArr == pArr[] == int pArr[]
예)
int a = 10;
int* b = &a; // b 주소는 a의 주소를 참조하게 된다
*b = 20; // b 주소에 20을 넣으면 a 값이 20으로 변경된다.
#include <iostream> // cin, cout #include <string> // string #include <stdio.h> // rand() #include <time.h> // time() #include <Windows.h> // Sleep() using namespace std; // void inputArr(int pArr[]) { // void inputArr(int* pArr) { void inputArr(int pArr[5]) { // cout << "&pArr[0] : " << &pArr[0] << endl; cout << endl; for (int i = 0; i < 5; i++){ // cout << "arr[" << i << "]번째 입력 : "; cin >> *(pArr + i); // *(pArr + i) == arr[0]이므로 위 코드와 아래 코드와 같다 cout << "arr[" << i << "]번째 입력 : "; cin >> pArr[i]; } } void outputArr(int* pArr) { for (int i = 0; i < 5; i++){ cout << "arr[" << i << "]번째 출력 : " << *(pArr + i) << endl; } } void main() { // 배열명 : 배열 전체를 가리키는(주소를 가지고) 포인터 상수 (arr 포인터 상수) int arr[5]; cout << "&arr[1] : " << &arr[1] << endl; cout << "arr + 1 : " << arr + 1 << endl; arr[0] = 0; arr[1] = 1; arr[2] = 2; arr[3] = 3; arr[4] = 4; for (int i = 0; i <= 4; i++ ){ cout << "arr[" << i << "] : " << arr[i] << endl; } cout << endl; // 메모리 번지 = arr + 2(2 위치 X 4byte(int)) // 아래 배열 포인터 메모리적 풀이 for (int i = 0; i <= 4; i++){ cout << "*(arr + " << i << ") : " << *(arr + i) << endl; } cout << endl; int* parr = arr; // 배열의 첫번째 요소만 저장 // 배열의 포인터 for (int i = 0; i <= 4; i++){ cout << "*(parr + " << i << ") : " << *(parr + i) << endl; } cout << endl; inputArr(arr); cout << endl; outputArr(arr); }
/* */ #include <iostream> // cin, cout #include <string> // string #include <stdio.h> // rand() #include <time.h> // time() #include <Windows.h> // Sleep() using namespace std; void main(){ /* 배열의 갯수를 몰라 허용 안함 int size; cout << "데이터의 갯수를 입력 : "; cin >> size; int data[size]; */ /* 정적 할당(stack 방식으로 저장) int data[5]; // 컴퓨터 로컬의 메모리 할당(자동 해제) 동적 할당(힙) data = new int[size]; // 사용자 메모리 할당(수동 해제:그렇지 않으면 메모리 누수) */ int size; cout << "데이터의 갯수를 입력 : "; cin >> size; int *data; // 포인터 변수 data = new int[size]; // }
댓글 0개
| 엮인글 0개
285개(7/15페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
165 | [C#] 네임스페이스 (namespace) | 푸딩뱃살 | 3405 | 2015.11.14 17:34 |
164 | [C#] 인터페이스 (Interface) | 푸딩뱃살 | 2196 | 2015.11.13 18:17 |
163 | [C/C++] Static(정적) 멤버 변수, 메소드 | 푸딩뱃살 | 2474 | 2015.11.13 10:32 |
162 | [C/C++] 추상 클래스 - 오버라이드 / 업/다운캐스팅(형변환) / virtual(가상함수) | 푸딩뱃살 | 4726 | 2015.11.12 02:00 |
161 | [C/C++] 메소드 오버라이드 | 푸딩뱃살 | 1993 | 2015.11.12 01:51 |
160 | [C#] 상속 (with Unity) | 푸딩뱃살 | 5454 | 2015.11.10 16:25 |
159 | [C/C++] 클래스 상속 | 푸딩뱃살 | 2246 | 2015.11.10 14:08 |
158 | [C/C++] 객체 활용 | 푸딩뱃살 | 2471 | 2015.11.09 21:28 |
157 | [C#] C# 객체 클래스 | 푸딩뱃살 | 3568 | 2015.11.08 15:51 |
156 | [C/C++] 생성자 / 소멸자 / 오버로드 | 푸딩뱃살 | 2203 | 2015.11.07 01:23 |
155 | [C/C++] 클래스 선언/정의, 객체 생성 | 푸딩뱃살 | 3568 | 2015.11.06 14:05 |
154 | [C/C++] 로또 프로그램 | 푸딩뱃살 | 2285 | 2015.11.06 12:00 |
153 | [C/C++] 2차원 동적 객체 배열 활용 | 푸딩뱃살 | 3650 | 2015.11.06 00:47 |
152 | [C/C++] 2차원 배열 | 푸딩뱃살 | 2403 | 2015.11.06 00:30 |
151 | [C/C++] 2차원 포인터 | 푸딩뱃살 | 2052 | 2015.11.06 00:26 |
150 | [C/C++] 동적할당 | 푸딩뱃살 | 1806 | 2015.11.05 11:23 |
149 | [C/C++] 당신의 프로그래밍에 디버깅 더하기 : Visual C++ 디버깅 기초에서 고급까지 | 푸딩뱃살 | 1452 | 2015.11.05 11:20 |
148 | [C/C++] 포인터와 배열의 이해 | 푸딩뱃살 | 2013 | 2015.11.04 23:54 |
>> | [C/C++] 포인터 | 푸딩뱃살 | 2193 | 2015.11.04 15:14 |
146 | [C/C++] 일반 함수와 메소드간의 차이 | 푸딩뱃살 | 2432 | 2015.11.03 23:38 |