포인터
C 프로그래밍 언어에서 *p는 포인터에 저장된 값을 나타내고 p는 값의 주소를 나타내며 포인터라고 합니다.
상수 정수* 및 int const* 포인터가 상수 int를 가리킬 수 있고 이 포인터가 가리키는 int 값을 변경할 수 없다고 말합니다. 그러나 포인터의 값은 상수가 아니며 다른 상수 int를 가리킬 수 있으므로 변경할 수 있습니다.
const int* const 포인터가 상수 int를 가리킬 수 있고 이 포인터가 가리키는 int 값을 변경할 수 없다고 말합니다. 그리고 포인터의 값도 변경할 수 없으며 이제 상수이며 다른 상수 int를 가리킬 수 없습니다.
기본 규칙은 구문 이름을 오른쪽에서 왼쪽으로 지정하는 것입니다.
// constant pointer to constant int const int * const // pointer to constant int const int *
예(C)
주석 처리된 잘못된 코드의 주석을 제거하고 오류를 확인하십시오.
#include <stdio.h>
int main() {
//Example: int const*
//Note: int const* is same as const int*
const int p = 5;
// q is a pointer to const int
int const* q = &p;
//Invalid asssignment
// value of p cannot be changed
// error: assignment of read-only location '*q'
//*q = 7;
const int r = 7;
//q can point to another const int
q = &r;
printf("%d", *q);
//Example: int const* const
int const* const s = &p;
// Invalid asssignment
// value of s cannot be changed
// error: assignment of read-only location '*s'
// *s = 7;
// Invalid asssignment
// s cannot be changed
// error: assignment of read-only variable 's'
// s = &r;
return 0;
} 출력
7