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

5
  • const int* const *plz is a double pointer and const int *const plz is a single pointer. So these are very different indeed. const int* const *plz is the odd one out. You need to count the number of asterisks. Commented Jan 5, 2015 at 11:04
  • http://www.thegeekstuff.com/2012/06/c-constant-pointers/ refer this Commented Jan 5, 2015 at 11:04
  • Duplicate? - stackoverflow.com/questions/1143262/… Commented Jan 5, 2015 at 11:05
  • @Dodu no that is not the same question, I also saw that question Commented Jan 5, 2015 at 11:07
  • Oh, alright. My bad. :) Commented Jan 5, 2015 at 11:09

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
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.
@DirkKoopman Yes coming to the usage of it I agree . But for understanding the above example is good IMO

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.