C++_2주차_정리

728x90

1. 포인터

-값의 개념과 주소의 개념을 구분해서 생각할 것 

#include <iostream> //c++의 표준 입출력 헤더파일(cin, cout객체 포함)
using namespace std; //이름 공간 설정 

int main() {
	
	int i = 0; //일반 변수
	int* ptr_i; //포인터 변수 
	int* ptr_x = &i;
	
	ptr_i = &i;
	//값의 개념
	cout << "Value of the integer variable i =" << i << endl;
	cout << "Value of the integer variable i =" << *ptr_i << endl;
	cout << "Value of the integer variable i =" << *ptr_x << endl;

	//주소의 개념
	cout << "Memory address of the integer variable i = " << &i << endl;
	cout << "Memory address of the integer variable i = " << ptr_i << endl;
	cout << "Memory address of the integer variable i = " << ptr_x << endl;
	
	getchar();
	return 0;
	
}

2. 배열 개념 

-각 배열 요소의 값 array[i]

-각 배열 요소의 주소 &array[i] 

#include <iostream> //c++의 표준 입출력 헤더파일(cin, cout객체 포함)
using namespace std; //이름 공간 설정 
#include<string>

int main() {
	int x[5] = { 0,1,2,3,4 };
	int y[5] = { 0 };
	double f[5] = { 0.1, 1.2, 2.3, 3.4, 4.5 };
	int i;

	char ch1[10] = { 'p', 'r', 'o', 'g', 'r', 'a', 'm', '\0' };
	char ch2[10] = "language";
	string st = "examples";

	for (i = 0; ch1[i] != '\0'; i++) {
		cout << ch1[i];
	}
	cout << endl << endl;
	cout << ch2 << endl << endl;
	cout << st;

	for (i = 0; i < 5; i++) { //for 돌면서 x[] 내부의 값과 주소 출력 
		cout << "Value of x[" << i << "J=" << x[i] << ", Address = " << &x[i] << endl;
	}
	cout << endl;
	for (i = 0; i < 5; i++) {
		cout << "Value of y[" << i << "]=" << y[i] << ", Address = " << &y[i] << endl;
	}
	cout << endl;
	for (i = 0; i < 5; i++) {
		cout << "Value of r[" << i << "]=" <<f[i] << ", Address = " << &f[i] << endl; 
    }
    
    getchar();
    getchar();
    return 0;
}

728x90

'C++, C언어 > [문법]_C++' 카테고리의 다른 글

C++_4주차_정리  (0) 2021.12.20
C++_3주차_정리  (0) 2021.12.20
C++_1주차_정리  (0) 2021.12.20
C++ 객체지향프로그래밍_Ch04정리  (0) 2021.09.27
C++ 객체지향프로그래밍_Ch03정리  (0) 2021.09.13