--포인터 선언 및 연산--
int num = 5;
int * ptr = # //0x00000000
ptr += 1; //0x00000004
/****************************************/
typedef struct
{
int arr[128];
int top;
}stack;
stack s;
stack * p = &s; //0x00000000
p += 1; //0x00000204
type형 포인터를 대상으로 한 n만큼의 증가 및 감소 시, n*sizeof(type)의 크기만큼 주소 값이 증가 및 감소한다.
--const 선언--
int num = 5;
int num1 = 3;
const int * ptr1 = #
int * const ptr2 = #
const int * const ptr3 = #
*ptr1 = 7; //fail
ptr1 = &num1; //success
*ptr2 = 8; //success
ptr2 = &num1; //fail
*ptr3 = 7; //fail
ptr3 = &num1; //fail
ptr1은 ptr1을 이용하여 num에 저장된 값 변경을 허용하지 않겠다는 것을 의미하고,
ptr2는 ptr2에 저장된 값을 변경하지 않겠다는 것을 의미하며,
ptr3는 위 두가지를 모두 포함한다.
--문자열 표현--
char str1[] = "hello";
const char * str2 = "hello"; //char * str = "hello"; vs2017에서는 안됨
str1[0] = 'H'; //success
*str1 = 'H'; //success
str1 = "Hello"; //fail
str2[0] = 'H'; //fail
*str2 = 'H'; //fail
str2 = "Hello"; //success
str1은 변수 형태의 문자열이고, str2는 상수 형태의 문자열이기 때문에 이와 같은 결과가 발생한다.
--이차원 배열--
int arr2[row][column];
int arr2[3][3] = {
1, 2, 3,
4, 5, 6,
7, 8, 9
};
int arr2[3][3] = {
{1}, //{1, 0, 0}
{4, 5}, //{4, 5, 0}
{7, 8, 9} //{7, 8, 9}
};
int arr2[3][3] = {
1, 2, 3
}; //나머지 부분은 0으로 채워짐
int arr1[] = {1, 2, 3, 4}; //배열의 길이를 명시하지 않아도 컴파일러가 알아서 삽입
int arr2[][] = {1, 2, 3, 4, 5, 6, 7, 8}; //컴파일 에러
int arr2[][4] = {1, 2, 3, 4, 5, 6, 7, 8}; //coulmn 길이는 반드시 명시해야 함, row 길이는 2
column의 길이를 반드시 명시해야 하는 이유는 이차원 배열을 대상으로 한 포인터 연산 시 얼마 만큼 연산할지 결정해야 하기 때문이다.
--더블 포인터--
--함수 포인터--
]
'C++' 카테고리의 다른 글
std::find() (0) | 2021.10.27 |
---|---|
Lvalue & Rvalue (0) | 2021.10.21 |
형변환 연산자 (0) | 2021.10.21 |
변수 (0) | 2021.10.21 |
서식문자 정리 (0) | 2021.10.21 |