러닝센터 5강.

러닝센터 5강.
로또 번호 추출기 다시 제작. - 과제
사이트 로토게임 코드 분석.

soen.kr -> c 코드 및 강좌
===[ 포인터 ] ==============================================

// [포인터 변수]
// - 일반 변수의 주소값를 저정하는 변수  
// [포인터 변수 문법]
// 1. 생성
//      자료형* 포인터변수;
// 2. 일반변수의 주소
//      &변수;
// 3. 포인터 변수에 주소 대입
//      포인터변수 = &일반변수  
// [레퍼런스 변수]
// - 일반 변수의 별칭으로 일반변수를 접근하는 두개의 이름이 부여됨
  
// [레퍼런스 변수 문법]
// 1. 생성
//      자료형& 레퍼런스변수 = 일반변수
 
// [기억클래스]
// - 자동 변수 : 함수의 선언된 매개변수로 함수 호출시 생성하고 호출이 종료될때 자동 소멸함
// - 지역변수 : 블럭안에서 선언된 변수로 사용범위가 블록 내부로 한정됨
// - 전역 변수 : 생략 (설명만)
// - 정적 변수 : 클래스 설명 부분에서 설명함
// - 클래스 멤버 변수 : 클래스 설명 부분에서 설명함



class Monster{
public:
int hp;
int damage;
int level;
};


void main(){

//정수형 변수 생성
int value1 = 100;
int value2 = 200;
cout << "value : " << value1 << endl;
cout << "value : " << value2 << endl;
// [주소 연산자]
// &변수명 : 현재 변수명을 가진 변수의 주소를 지칭함
cout << "value 변수의 주소 : " << &value1 << endl; //변수를 주소의 값으로
cout << "value 변수의 주소 : " << &value2 << endl;

//[ 포인터 연산자 ]
//*(주소) : 주소에 해당하는 변수를 지칭함
cout << "value1 주소를 통한 값 접근 : " << *(&value1) << endl; // 주소를 변수의 값으로
cout << "value2 주소를 통한 값 접근 : " << *(&value2) << endl;
cout << "*(&(*(&value2))) : " << *(&(*(&value2))) << endl;

Monster mon; // 객체 변수
mon.hp = 100;// 맴버 변수
mon.level = 1;
mon.damage = 20;
cout << "mon의 객체 변수의 주소 : " << &mon << endl;// 해당 변수의 메모리 주소.
cout << "mon의 객체 hp 변수의 주소 : " << &(mon.hp) << endl;// 해당 맴버변수의 메모리 주소.
cout << "mon의 객체 level 변수의 주소 : " << &(mon.level) << endl;// 해당 맴버변수의 메모리 주소.
cout << "mon의 객체 damage 변수의 주소 : " << &(mon.damage) << endl;// 해당 맴버변수의 메모리 주소.

cout << "mon의 객체 damage  변수의 실제 값  : " << (&(mon.damage)) << endl;// 해당 맴버변수의 메모리 주소.

// &변수 앞에 붙고 // 메모리 주소를 참조한다
// *주소앞에 붙는다 // 메모리 주소를 원래 변수의 값으로 변환한다/

cout << "mon 객체의 크기  : " << sizeof(mon) << endl;// 객체의 크기
cout << "mon.damage  : " << sizeof(mon.damage) << endl;// 객체의 크기


//int* => 주소를 저장할 수 있는 자료형 int형 주소 타입.
int* pvalue1 = &value1; //int*를 사용하여 주소값을 pvalue1에 저장한다.
cout << "pvalue1 : " << pvalue1 << "&value1" << &value1 << endl;
cout << "*pvalue1 : " << *pvalue1 << "*(&value1)" << *(&value1) << endl;

int *pvalue2 = &value2;
cout << "pvalue2 : " << pvalue2 << "&value2" << &value2 << endl;
cout << "*pvalue2 : " << *pvalue2 << "*(&value2)" << *(&value2) << endl;

// &를 붙이면 메모리의 주소를 참조
// *주소의 실체를 반환 = > 변수

Monster* pmonster; // 몬스터 객체의 메로리 주소를 저장할 수 있는 포인터 변수
pmonster = &mon; // 객체의 주소를 대입

cout << "value1(mon) : " << sizeof(value1) << endl;  //크기(바이트) 출력 int 타입이므로 4바이트
cout << "pvalue1(mon) : " << sizeof(pvalue1) << endl;

cout << "sizeof(mon) : " << sizeof(mon) << endl; // 객체의 크기가 12바이트이기 떄문에 12바이트를 반환
cout << "sizeof(pmonster) : " << sizeof(pmonster) << endl; // 주소의 크기가 4바이트를 넘지 않기 때문에.


//객체를 접근하는 것이 실체면 .을 이용 메모리 주소면 -> 을 이용
cout << "pmonster : " << pmonster << ", & mon" << &mon << endl;
cout << "mon.hp : " << mon.hp << endl;
cout << "mon.level : " << (&mon)->hp << endl;
cout << "mon.damage : " << pmonster ->hp << endl;

}


  int value; == *(&value);

// 변수에 &를 붙이면 메모리의 주소를 참조

// 메모리 주소에 *주소의 실체를 반환 = > 변수
& 레퍼런스 사용시 변수의 주소선언 없이 실체로 사용한다
배열은 주소 연산을 사용하여 접근한다. ( 메모리 주소)
배열은 기본적으로 메모리 주소를 참조한다.
함수의 매개변수로 배열을 받을때

// 포인터를 사용한 배열의 참조
void printArr1(int* arr){
cout << "main().arr  의 값" << arr << endl;
for (int i = 0; i < 5; i++){
cout << i + 1 << "번째 값 출력" << arr[i] << endl;
}
}
// 레퍼런스를 이용한 배열의 참조
void printArr2(int arr[5]){
//cout << "main().arr  의 값" << arr << endl;
for (int i = 0; i < 5; i++){
cout << i + 1 << "번째 값 출력" << arr[i] << endl;
}
}
void inputArr(int arr[]){
for (int i = 0; i < 5; i++){
cout << i + 1 << "번째 값을 입력";
cin >> arr[i];
}
}
void main() {
//  arr => 배열명 => 상수 => 배열의 시작 주소값
int arr[5] = { 0, 1, 2, 3, 4 };

inputArr(arr); // 함수 실행
printArr1(arr);// 함수 실행
}


  int value; == *(&value); 
함수 사용시 함수 실행 후에 함수 안에서 선언한 변수는 사라지고 해당값이 함수 밖으로 전달 되지 않으므로
포인터를 사용하여 실제 메모리 주소를 함수로 넘겨주어 값이 사라지지 않고 참조 할 수 있도록 한다.
다른 메모리 영역에 있는 데이터를 집적 접근한다. 

객체도 레퍼런스화 시킬 수 있다.


#include <iostream> // 콘솔 입출력
#include <stdlib.h> // 랜덤 모듈
#include <time.h> // 타임 추출
#include <string>
#include <CoreWindow.h>

using namespace std;

class Monster{
public:
int hp;
int damage;
int level;
};


void swapPoint(int* type1, int* type2){

cout << "swapPoint 함수의 value1 : " << type1 << endl;
cout << "swapPoint 함수의 value2 : " << type2 << endl;

cout << "swapPoint 함수의 value1 : " << *type1 << endl;
cout << "swapPoint 함수의 value2 : " << *type2 << endl;

int temp = *type1;
*type1 = *type2;
*type2 = temp;
}


void swapObject(Monster* object1, Monster* object2){
Monster temp;
temp = *object1;
*object1 = *object2;
*object2 = temp;
}
void main(){


Monster monster1;
Monster monster2;


monster1.level = 1;
monster1.damage = 10;
monster1.hp = 100;

monster2.level = 2;
monster2.damage = 20;
monster2.hp = 200;

cout << "몬스터1의 hp" << monster1.hp << endl;
cout << "몬스터2의 hp" << monster2.hp << endl;


swapObject(&monster1, &monster2);
cout << "몬스터1의 hp" << monster1.hp << endl;
cout << "몬스터2의 hp" << monster2.hp << endl;




//value1의 값과 value2의 값을  교체하시오
/*
int temp = value1;
value1 = value2;
value2 = temp;
*/



}

===[ 2차원 배열 ]===========================================



댓글

이 블로그의 인기 게시물

날짜 시간 시간차 시간 계산 하기

코루틴에서 CallBack 함수 적용하기

C++ 언어 퍼센트 구하는 방법 / 기본 언어 퍼센트 구하는 방법