What is the difference between constint*, constint*, and int const*?

Asked 1 years ago, Updated 1 years ago, 66 views

const int* const int * const int const *

What's the difference between these three?

c++ const declaration pointer

2022-09-21 23:20

1 Answers

The const position can be written as follows:

If you twist it a lot, you can use it like this

I'll explain it together in the example

int number = 3;
int anotherNumber = 4;
const int constNumber = 5;

int* ptr0 = &number;
*ptr0 = 4; // Possible
ptr0 = &anotherNumber; // possible
ptr0 = &constNumber;//int* cannot point to constant

constint* ptr1 = & number; // possible
*ptr1 = 5; //ptr1 is pointing to constant, not possible
ptr1 = &anotherNumber; // possible
ptr1 = &constNumber;// Possible

int *const ptr2; // impossible. Const pointer must be initialized at the same time as declaration

int *const ptr3 = & number; // possible
*ptr3 = 6; // possible
Cannot change pointing target because ptr3 = &anotherNumber; //impossible const pointer

For more information, see Clockwise/Spiral Rule


2022-09-21 23:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.