0

I am reading book Pointers on C by Kenneth Reek book talks about constant pointers

what are constant pointer to integer?

In them pointer is constant (what that means?) its value cannot be changed (shouldn't its value be value of cpi=5 5 is the value of constant which should be read as constant integer Or is constant one thing and integer is another -- please clarify) book continue saying but u are free to modify integer to which points to so basically I can do cpi=5 and in next line cpi=19 then why do I need them what it makes constant? to have constant pointer what we achieve.

int * const cpi;

The auther also says int constant *pci is a pointer to constant integer. and so I can change the pointer value (Q: how can I change the pointer value can u give examples)

user786
  • 3,902
  • 4
  • 40
  • 72
  • 3
    Does this answer your question? [What is the difference between const int\*, const int \* const, and int const \*?](https://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-and-int-const) – rveerd May 11 '21 at 10:50

3 Answers3

1

This int * const cpi with const on the right side of the * means that the pointer itself cannot change to point somewhere else. There aren't many uses for it, look-up tables based on pointers is pretty much the only somewhat common use-case. They are also used for storing pointers in non-volatile flash memory on embedded systems, rather than RAM.

const int* cpi is the more common use, meaning that the pointed-at data is read-only. This is utilized when writing const correctness function interfaces, among other things.

To make things even more confusing, C allows you to re-order the type qualifier const, and the type specifier int, so int const* means the very same thing as const int*.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • And other strange irrational rules also allow us to write haywire code... `const long const long const const* const const p;` is valid C. – Lundin May 11 '21 at 10:54
  • Can u give me an example code of constant pointer usage. – user786 May 11 '21 at 10:58
  • @user786 It's easier to speak of read-only pointers vs pointers to read-only variables, then there's no confusion. An example of the former is a state machine or `switch` replacement in the form of an array of function pointers. You don't want the behavior to change in run-time so you'd do something like `myfunc_t* const table[] = ...` – Lundin May 11 '21 at 13:26
  • This rule belongs to those C features I don't use because my brain refuses to remember such a counter intuitive rule (`const int *` makes me think that's the pointer to be constant). – Roberto Caboni May 12 '21 at 06:55
  • @RobertoCaboni You could also do `const int * const` in most cases. Then both the pointer and the pointed-at data is read-only and you don't have to worry about where `const` goes. – Lundin May 12 '21 at 08:39
1
#include <stddef.h> // NULL

int main(void) {
    int foo = 42;

    int *ip = &foo;               // can change both `ip` and `*ip`
    int *const icp = &foo;        // cannot change `icp`, can change `*icp`
    const int *cip = &foo;        // can change `cip`, cannot change `*cip`
    const int *const cicp = &foo; // cannot change `cicp`, cannot change `*cicp`

    ip = NULL; // ok
    ip = &foo; // so that next line works
    *ip = -1;  // ok

//  icp = NULL; // nope
    *icp = -1;  // ok

    cip = NULL; // ok
//  *cip = -1;  // nope

//  cicp = NULL; // nope
//  *cicp = -1;  // nope

    int *const *j; // can change `j`, cannot change `*j`, can change `**j`;
}
pmg
  • 106,608
  • 13
  • 126
  • 198
  • `*icp = -1; // ok` causes segFault – user786 May 12 '21 at 04:37
  • 1
    Yes, my bad @user786. `icp` was initialized with `NULL`. Code edited, thank you. – pmg May 12 '21 at 06:47
  • my compiler gives me error when I even try to assign value like `*icp=5//icp=6 //..` can changes costs read only its gcc // icp is const pointer – user786 May 12 '21 at 06:54
  • I only get "unused variable" warnings ( see https://godbolt.org/z/nxWK9vY6Y ). What is your compiler and options? – pmg May 12 '21 at 07:27
  • Maybe you mistyped some statement? `*icp = 5;` should compile... `icp = 6;` should not compile. – pmg May 12 '21 at 07:48
0

There is a difference between the value of a pointer (1) and the value which a pointer points to (2). So there's also a difference between pointer to constant (applies to (2)) and a constant pointer (applies to (1)). The best explanation is the "Constness" section of Pointer declaration page in the official docs.


But in general:

(1) Pointer is a variable that keeps the address of a slot in memory. Under this address can be some value (foe example an integer) which can be accessed using the dereference operator *. So when changing the value of a pointer you are changing the address in memory which it points to, but you won't be able to do it if you have a constant pointer to object: int* const myPointer

(2) Value which a pointer points to is just a value in a memory. It can be also value of some variable. So you can change the value which a pointer points to, by using the dereference operator *:

int myValue = 5;
int* myPointer = &myValue;

std::cout << *myPointer << std::endl;  // prints: 5

*myPointer = 13;  // changing the value of a variable which pointer points to

std::cout << myValue << std::endl;  // prints: 13

But you won't be able to do it if you have a pointer to constant: const int* myPointer