Pointer to a constant pointer

If I create a global array of const values ​​e.g.

const int SOME_LIST[SOME_LIST_SIZE] = {2, 3, 5, 7, 11}; 

Is it possible for SOME_LIST to be modified in any way?

How can I write this so that SOME_LIST points to a place in const memory and is the const pointer itself (i.e. cannot be specified elsewhere)?

+6
source share
2 answers

The way you do this is right.

In addition, you do not need to provide SOME_LIST_SIZE ; C ++ will automatically detect this from the initializer.

+8
source

There are 3 basic example pointers that include the keyword const. (See Link)

First: Declaring a pointer to a constant variable. The pointer can move and change what it points to, but the variable cannot be changed.

 const int* p_int; 

Secondly: declaring a pointer "fixed" to a variable. The pointer is "fixed", but data is subject to change. This pointer must be declared and assigned, otherwise it can point to NULL and be fixed there.

 int my_int = 100; int* const constant_p_int = &my_int; 

Third: declaring a fixed pointer to persistent data.

 const int my_constant_int = 100; (OR "int const my_constant_int = 100;") const int* const constant_p_int = &my_constant_int; 

You can also use this.

 int const * const constant_p_int = &my_constant_int; 

Another good link see here. I hope this helps, although by writing this, I understand that your question has already been answered ...

+11
source

Source: https://habr.com/ru/post/922303/


All Articles