program story

const char * 및 char const *-동일합니까?

inputbox 2020. 10. 9. 11:11
반응형

const char * 및 char const *-동일합니까?


내 이해에서 const수식어는 오른쪽에서 왼쪽으로 읽어야합니다. 그것으로부터 나는 그것을 얻습니다.

const char*

char 요소는 수정할 수 없지만 포인터 자체는 수정할 수있는 포인터입니다.

char const*

mutable문자에 대한 상수 포인터 입니다.

하지만 다음 코드에 대해 다음과 같은 오류가 발생합니다.

const char* x = new char[20];
x = new char[30];   //this works, as expected
x[0] = 'a';         //gives an error as expected

char const* y = new char[20];
y = new char[20];   //this works, although the pointer should be const (right?)
y[0] = 'a';         //this doesn't although I expect it to work

그래서 .. 어느 거지? 내 이해 또는 컴파일러 (VS 2005)가 잘못 되었습니까?


실제로 표준에 따라 왼쪽에const 있는 요소를 직접 수정합니다 . 의 사용 선언의 시작 부분에 그냥 편리 정신 바로 가기입니다. 따라서 다음 두 문은 동일합니다.const

char const * pointerToConstantContent1;
const char * pointerToConstantContent2;

포인터 자체가 수정되지 않도록 const하려면 별표 뒤에 와야합니다.

char * const constantPointerToMutableContent;

포인터와 포인터가 가리키는 내용을 모두 보호하려면 두 개의 const를 사용하십시오.

char const * const constantPointerToConstantContent;

저는 개인적 으로 수정하지 않으려는 부분 뒤에 항상 const를 두어 포인터가 일정하게 유지하고 싶은 부분이더라도 일관성을 유지하도록 채택했습니다 .


둘 다 동일하기 때문에 작동합니다. 이것에 혼란 스러울 수 있습니다.

const char*  // both are same
char const*

char* const  // unmutable pointer to "char"

const char* const  // unmutable pointer to "const char"

[이것을 기억하기 위해 여기 간단한 규칙이 있습니다. '*'는 전체 LHS에 먼저 영향을 미칩니다. ]


규칙이 다음과 같기 때문입니다.

규칙 : const왼쪽에 아무것도 없으면 왼쪽으로 묶고 오른쪽으로 묶습니다. :)

따라서 다음과 같이보십시오.

(const --->> char)*
(char <<--- const)*

둘 다 동일합니다! 아, 그리고 --->><<---운영하지, 그들은 단지 무엇을 보여 const결합합니다.


( 2 간단한 변수 초기화 질문에서 )

다음에 관한 정말 좋은 경험 법칙 const:

오른쪽에서 왼쪽으로 선언문을 읽으십시오.

(Vandevoorde / Josutiss "C ++ 템플릿 : 전체 가이드"참조)

예 :

int const x; // x is a constant int
const int x; // x is an int which is const

// easy. the rule becomes really useful in the following:
int const * const p; // p is const-pointer to const-int
int const &p;        // p is a reference to const-int
int * const * p;     // p is a pointer to const-pointer to int.

이 규칙을 따랐기 때문에 그런 선언을 다시는 잘못 해석하지 않았습니다.

(: sisab retcarahc-rep a no ton, sisab nekot-rep a no tfel-ot-thgir naem I hguohT : tidE


내가 항상 해석하려고하는 방법은 다음과 같습니다.

char *p

     |_____ start from the asterisk. The above declaration is read as: "content of `p` is a `char`".

char * const p

     |_____ again start from the asterisk. "content of constant (since we have the `const` 
            modifier in the front) `p` is a `char`".

char const *p

           |_____ again start from the asterisk. "content of `p` is a constant `char`".

도움이 되었기를 바랍니다.


두 경우 모두 상수 문자를 가리키고 있습니다.

const char * x  //(1) a variable pointer to a constant char
char const * x  //(2) a variable pointer to a constant char
char * const x  //(3) a constant pointer to a variable char
char const * const x //(4) a constant pointer to a constant char
char const * const * x //(5) a variable pointer to a constant pointer to a constant char
char const * const * const x //(6) can you guess this one?

기본적으로 const는 왼쪽에있는 중간에 적용되지만 (1)에서와 같이 앞에 아무것도 없으면 오른쪽에 즉시 적용 할 수 있습니다.

참고 URL : https://stackoverflow.com/questions/8091770/const-char-and-char-const-are-they-the-same

반응형