0
int *const plz;

means that I will not change where the pointer is pointing to, (ie increment or decrement the address)

const int *plz

means I will not change the variable the pointer is pointing to through the pointer

const int* const *plz

means both

I got a question

I just saw a function which looks like this

check_plz(const int *const plz)

what exactly does this mean, other than that the address cannot be incremented or decremented, if it also means that I cannot change the variable, why is the second * operand missing? Thank you

Mcs
  • 534
  • 1
  • 5
  • 14

1 Answers1

1
const int *const plz

Here plz is a constant pointer to a constant int variable

The below example might help you

const int *const *plz

Here plz is a double pointer so it can hold the address of a pointer.

#include <stdio.h>

int main(void) {
    const int a=10;
    const int *const p = &a;
    const int *const *q = &p; 
    printf("%d\n",*p);
    printf("%p\n",(void *)p);
    printf("%p\n",(void *)*q);
    printf("%d\n",**q);

    return 0;
}

So with this each of your used variables like p q a are just read-only now.

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • so const int *const *ptr is a constant pointer to a variable but it is not allowed that we change the variable through the pointer. Const int const *ptr is a constant pointer to a constant variable – Mcs Jan 05 '15 at 11:11
  • Does one *really* need to use `const` with such gay abandon as in the examples above. We are writing in C, not C++. I cannot remember a time in the last 30 years when a `const` keyword would have saved me some debugging time, whereas overuse of `const` by others has caused much anquish (and occasionally `#define const`) to sort out problems. – Dirk Koopman Jan 05 '15 at 12:32
  • @DirkKoopman Yes coming to the usage of it I agree . But for understanding the above example is good IMO – Gopi Jan 05 '15 at 13:05