0

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.

hwkd
  • 2,411
  • 3
  • 18
  • 27
  • possible duplicate of [what is the difference between const int\*, const int \* const, int const \*](http://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-int-const) – mskfisher Oct 23 '14 at 12:48
  • Very similar, but one is "What are they?" the other is "When do we use them?" – Baldrickk Oct 23 '14 at 12:50
  • `const T*` is the most useful and common. It allows a function to read data only. By tightly controlling which parts of your program can modify data, it's easier to design your program and debug. – Neil Kirk Oct 23 '14 at 12:53
  • possible duplicate of [Sell me on const correctness](http://stackoverflow.com/questions/136880/sell-me-on-const-correctness) – risingDarkness Oct 23 '14 at 13:14

3 Answers3

1

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

Laura Maftei
  • 1,863
  • 1
  • 15
  • 25
0

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.

Baldrickk
  • 4,291
  • 1
  • 15
  • 27
0

Since you're asking for use examples:

  • A typical use for pointer to constant char is for passing around pointers to read-only memory (e.g. 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 (i.e. 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.

Marco A.
  • 43,032
  • 26
  • 132
  • 246