Ex49-포인터 변수 상수화 방법 2

CODEDRAGON Development/C, C++

반응형

   

포인터 변수 상수화 방법 2

포인터 변수를 통해 메모리 공간의 값을 변경하지 못하게 하기

   

   

소스코드

   

#include <stdio.h>

int main(void) {
        char x='A';
        char y='B';
        const char* p=&x;       // *p
상수화, const char* p=&x범위가 상수화

        printf("%c \n", *p);
        printf("%c \n", x);

        p=&y;   //
주소는 변경가능
        printf("%c \n", *p);
        printf("%c \n", y);

        x='Z';
        y='Y';
//      *p='X';                         //
에러(값은 변경불가)

        printf("%c \n", x);
        printf("%c \n", y);
        printf("%c \n", *p);

        return 0;
}


 

   

출력결과

   

반응형