program tip

상수 포인터 대 상수에 대한 포인터

radiobox 2020. 7. 30. 10:17
반응형

상수 포인터 대 상수에 대한 포인터


나는 차이점을 알고 싶다

const int* ptr;

int * const ptr; 

어떻게 작동하는지.

이것을 이해하거나 기억하기가 매우 어렵습니다. 도와주세요.


const int* ptr; 

유형에 ptr대한 포인터를 선언 합니다 const int. ptr자체적으로 수정할 수는 있지만에 의해 지정된 개체는 ptr수정되지 않습니다.

const int a = 10;
const int* ptr = &a;  
*ptr = 5; // wrong
ptr++;    // right  

동안

int * const ptr;  

유형에 ptr대한 const포인터를 선언 합니다 int. ptr에 의해 지시 된 객체는 수정할 수 없지만가 가리키는 객체 ptr입니다.

int a = 10;
int *const ptr = &a;  
*ptr = 5; // right
ptr++;    // wrong

일반적으로 나는 읽기 쉽고 이해하기 쉽도록 다음과 같은 선언을 선호합니다 (오른쪽에서 왼쪽으로 읽음).

int const  *ptr; // ptr is a pointer to constant int 
int *const ptr;  // ptr is a constant pointer to int

const int * ptr;

지시 된 데이터는 일정하고 불변이지만 포인터는 그렇지 않음을 의미합니다.

int * const ptr;

는 포인터가 일정하고 불변이지만 뾰족한 데이터는 그렇지 않음을 의미합니다.


1) 상수 포인터 :포인터 유형은 가리키는 주소를 변경할 수없는 포인터입니다. 이는 변수를 가리키는 포인터가 있거나 해당 변수의 주소를 저장한다고 가정합니다. 이제 포인터를 다른 변수로 가리 키거나 포인터를 다른 변수의 주소를 저장하려고하면 상수 포인터 로이 기능을 사용할 수 없습니다.

상수 포인터는 다음과 같이 선언됩니다 : int *const ptr( 'const'의 위치는 포인터 'ptr'을 상수 포인터로 만듭니다)

2) 포인터를 가리키는 포인터 : 이 포인터 유형은 가리키는 값을 변경할 수없는 포인터입니다. 즉, 주소를 보유한 변수의 값을 변경할 수 없습니다.

상수에 대한 포인터는 다음과 같이 선언됩니다. const int *ptr'const'의 위치는 포인터 'ptr'을 상수에 대한 포인터로 만듭니다.

상수 포인터

#include<stdio.h>

int main(void)
{
    int a[] = {10,11};
    int* const ptr = a;

    *ptr = 11;

    printf("\n value at ptr is  : [%d]\n",*ptr);
    printf("\n Address pointed by ptr  : [%p]\n",(unsigned int*)ptr);

    ptr++;
    printf("\n Address pointed by ptr  : [%p]\n",(unsigned int*)ptr);

    return 0;
}

위의 코드를 컴파일하면 컴파일러는 다음과 같이 불평합니다.

practice # gcc -Wall constant_pointer.c -o constant_pointer
constant_pointer.c: In function ‘main’:
constant_pointer.c:13: error: increment of read-only variable ‘ptr’

따라서 우리는 컴파일러가 상수 포인터로 유지되는 주소를 변경할 수 없다는 불평을 매우 명확하게 볼 수 있습니다.

상수에 대한 포인터

#include<stdio.h>

int main(void)
{
    int a = 10;
    const int* ptr = &a;


    printf("\n value at ptr is  : [%d]\n",*ptr);
    printf("\n Address pointed by ptr  : [%p]\n",(unsigned int*)ptr);

    *ptr = 11;

    return 0;
}

이제 위의 코드가 컴파일되면 컴파일러는 다음과 같이 불평합니다.

practice # gcc -Wall pointer_to_constant.c -o pointer_to_constant
pointer_to_constant.c: In function ‘main’:
pointer_to_constant.c:12: error: assignment of read-only location ‘*ptr’

따라서 여기서도 컴파일러는 상수에 대한 포인터가 가리키는 변수의 값을 변경하도록 허용하지 않습니다.

인용


추천 This Thread

상수 포인터

Lets first understand what a constant pointer is. A constant pointer is a pointer that cannot change the address its holding. In other words, we can say that once a constant pointer points to a variable then it cannot point to any other variable.

A constant pointer is declared as follows :
<type of pointer> * const <name of pointer>
An example declaration would look like :
int * const ptr;
Lets take a small code to illustrate these type of pointers :

#include<stdio.h>

int main(void)
{
    int var1 = 0, var2 = 0;
    int *const ptr = &var1;
    ptr = &var2;
    printf("%d\n", *ptr);

    return 0;
} 

In the above example :

  • We declared two variables var1 and var2
  • A constant pointer ‘ptr’ was declared and made to point var1
  • Next, ptr is made to point var2.
  • Finally, we try to print the value ptr is pointing to.

Pointer to Constant

As evident from the name, a pointer through which one cannot change the value of variable it points is known as a pointer to constant. These type of pointers can change the address they point to but cannot change the value kept at those address.

A pointer to constant is defined as : const <type of pointer>* <name of pointer> An example of definition could be : const int* ptr; Lets take a small code to illustrate a pointer to a constant :

 #include<stdio.h>

int main(void)
{
    int var1 = 0;
    const int* ptr = &var1;
    *ptr = 1;
    printf("%d\n", *ptr);

    return 0;
} 

In the code above :

  • We defined a variable var1 with value 0
  • we defined a pointer to a constant which points to variable var1
  • Now, through this pointer we tried to change the value of var1
  • Used printf to print the new value.

const int* ptr;

is a pointer to constant (content). You are allowed to modify the pointer. e.g. ptr = NULL, ptr++, but modification of the content is not possible.

int * const ptr;

Is a constant pointer. The opposite is possible. You are not allowed to modify the pointer, but you are allowed to modify what it points to e.g. *ptr += 5.


int i;
int j;

int * const ptr1 = &i;

The compiler will stop you changing ptr1.

const int * ptr2 = &i;

The compiler will stop you changing *ptr2.

ptr1 = &j; // error
*ptr1 = 7; // ok

ptr2 = &j; // ok
*ptr2 = 7; // error

Note that you can still change *ptr2, just not by literally typing *ptr2:

i = 4;
printf("before: %d\n", *ptr2); // prints 4
i = 5;
printf("after: %d\n", *ptr2); // prints 5
*ptr2 = 6; // still an error

You can also have a pointer with both features:

const int * const ptr3 = &i;

ptr3 = &j; // error
*ptr3 = 7; // error

Please refer the following link for better understanding about the difference between Const pointer and Pointer on a constant value.

constant pointer vs pointer on a constant value


const int* ptr; here think like *ptr is constant and *ptr can't be change again

int * const ptr; while here think like ptr as a constant and that can't be change again

참고URL : https://stackoverflow.com/questions/21476869/constant-pointer-vs-pointer-to-constant

반응형