简体   繁体   中英

Where and why do we use “pointers that point to constants”, “constant pointers”, and “constant pointers that point to constants”?

so if I have something like this in C++:

char A_char = 'A';
char * myPtr = &A_char;

const char * myPtr = &char_A; //pointers that point to constants
char * const myPtr = &char_A; //constant pointers
const char * const myPtr = &char_A; //constant pointers that point to constants

I was wondering where and why we use "pointers that point to constants", "constant pointers", and "constant pointers that point to constants" in programming. I know the differences between them and their syntax, but I have no idea where and why we use them. Would you be able to explain? Thanks guys.

A constant pointer is a pointer that cannot change the address it is holding.

<type of pointer> * const <name of pointer>

A pointer through which one cannot change the value of variable it points is known as a pointer to constant.

const <type of pointer>* <name of pointer>

A constant pointer to constant is a pointer that can neither change the address its pointing to and nor it can change the value kept at that address.

const <type of pointer>* const <name of pointer>

You can find more details of their usage here: http://www.codeguru.com/cpp/cpp/cpp_mfc/general/article.php/c6967/Constant-Pointers-and-Pointers-to-Constants.htm

If the data shouldn't change:
Use a pointer to constant data.
const char * myPtr = &char_A;

If the data can change but the memory pointed at shouldn't:
Use a constant pointer:
char * const myPtr = &char_A;

If the data shouldn't change and the memory pointed at shouldn't change either:
Use a constant pointer to constant data:
const char * const myPtr = &char_A;

There really isn't much more to it than that.

Since you're asking for use examples:

  • A typical use for pointer to constant char is for passing around pointers to read-only memory (eg string literals). That memory can be dereferenced but not modified (undefined behavior)

     const char* myPtr = "this is read only memory"; 
  • A constant pointer to char might be used to pass around a buffer location which can be modified, but the location cannot be changed (ie you need to specifically use that memory area)

     char * const myPtr = some_buffer_location; printBuffer(myPtr); void copyToBuffer(char * const &pointer_to_buffer /* Reference to pointer */) { const char* my_contents = "this is my content"; strcpy(pointer_to_buffer, my_contents); // Buffer contents can be changed // pointer_to_buffer = NULL; - error! Not assignable } 
  • The combination of the above can be used to pass around a read-only memory area and ensure the address pointed remains the same and its contents remain the same.

Achieving full const-correctness is extremely hard , but if one is wishing to pursue that objective a proper combination of the above flags is necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM