1 개요[ | ]
- C언어 포인터와 상수 관계
2 대상체 상수[ | ]
- 대상체가 상수이기 때문에 값을 변경할 수 없다.
- 상수인 대상체의 값 변경을 시도하면 컴파일 오류가 발생한다.
2.1 포인터 변수 1[ | ]
C
Copy
const int *p = &i;
C
CPU
0.0s
MEM
14M
0.0s
Copy
#include <stdio.h>
void main() {
int i = 1;
const int *p = &i;
*p = 2; // error: assignment of read-only location ‘*p’
}
runbox.c: In function 'main': runbox.c:5:8: error: assignment of read-only location '*p' *p = 2; // error: assignment of read-only location ‘*p’ ^ Command exited with non-zero status 1 bash: ./a.out: No such file or directory
2.2 포인터 변수 2[ | ]
C
Copy
int const *p = &i;
C
Copy
#include <stdio.h>
void main() {
int i = 1;
int const *p = &i;
*p = 2; // error: assignment of read-only location ‘*p’
}
Loading
2.3 포인터 변수 3[ | ]
C
Copy
const int const *p = &i;
C
Copy
#include <stdio.h>
void main() {
int i = 1;
const int const *p = &i;
*p = 2; // error: assignment of read-only location ‘*p’
}
Loading
3 대상체 변수[ | ]
3.1 포인터 상수[ | ]
- 포인터가 상수이기 때문에 값을 변경 할 수 없다.
C
Copy
int * const p = &i;
C
Copy
#include <stdio.h>
void main() {
int i = 1;
int * const p = &i;
p++; // error: increment of read-only variable ‘p’
}
Loading
3.2 포인터 상수[ | ]
- 포인터가 상수이기 때문에 값을 변경 할 수 없다.
C
Copy
const int * const p = &i;
C
Copy
#include <stdio.h>
void main() {
int i = 1;
const int * const p = &i;
p++; // error: increment of read-only variable ‘p’
}
Loading
4 같이 보기[ | ]
편집자 Jmnote Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.
- 분류 댓글:
- C (7)
C, C++ 주석 ― YkhwongC, C++ 주석 ― John JeongC, C++ 주석 ― JmnoteC, C++ 주석 ― John JeongC언어 연결리스트 구현 ― 돌멩이C언어 연결리스트 구현 ― John JeongC언어 연결리스트 구현 ― 돌멩이