6

I have this function signature:

void myFunction(int *const ptr);

What's the point of the const keyword in this particular context?

Even if ptr wasn't a const variable, I couldn't modify to what address it points to, because it's passed by value, so if I had another function like this:

void myAnotherFunction(int *ptr);

And inside its implementation did something like this:

//...
ptr = malloc(1023);
//...

This wouldn't affect the passed value (outside this function, of course).

So the question is: what's the point of using myFunction() signature instead of myAnotherFunction() one? (beside that you get a compile-time error).

mariusmmg2
  • 713
  • 18
  • 37
  • 1
    This can be useful for refactoring. For example, extracting a block of code into a function where ptr used to be a local variable. The `const` is a good way to check that `ptr` wasn't changed during that block, which would have made the new code have different behavior. – Vaughn Cato Oct 03 '15 at 18:46
  • Note: the minimal prototype is still `void myFunction(int *)`; it doesn't care about `int *const ptr` because it's necessarily going to be erased at the end of the function, so it ignores that; it affects what's scoped *within* the function. – Neil Jan 24 '22 at 05:18

2 Answers2

6

ptr = malloc(1023); This wound't affect the passed value (outside this function, of course).

To sum up. Indeed that way original pointer would not be affected (it would still point to same object because ptr is copy of the original pointer), but you can still make that pointer point to a different object inside myAnotherFunction and change value of that object through that pointer. e.g. in myAnotherFunction you could do:

ptr = &y;
*ptr = 9;

Whereas in the first case when you have a constant pointer, you can't assign a new address to it, but you can still change the value of object to which it points using dereferencing. A constant pointer means you can't assign a new address to it, pointer to a constant object means you can't change value of object to which it points, but you can assign new address to the pointer.

Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
  • You said: `Meaning you can't assign new address to that variable`. Ok, and lets say that the pointer wasn't `const`, how would you assign a new address to it? About this is my question. – mariusmmg2 Oct 03 '15 at 18:44
  • @MariusMarusanici: Normally, as you would usually do (just that change won't be visible in the original pointer) – Giorgi Moniava Oct 03 '15 at 18:44
  • 1
    So if the change won't be visible in the original pointer, what's the point of that `const` keyword? – mariusmmg2 Oct 03 '15 at 18:46
  • Ok, great, I was somehow thinking only about the original passed pointer state. – mariusmmg2 Oct 03 '15 at 18:58
  • @MariusMarusanici: Glad if this helped. Always parameters are passed by value so ptr is copy of original pointer, and just ptr=newAddress, doesn't change original pointer (but if this is const you can't even write that) – Giorgi Moniava Oct 03 '15 at 19:01
5

It might make more sense if you consider that the function

void myFunction(int *const ptr);

might be manipulating many pointers, and assigning them and reassigning them, and an invariant of the algorithm it is executing is that the input pointer should never be changed. Then, labeling it const might be very helpful, or at least make you feel more confident that its been implemented correctly.

Chris Beck
  • 15,614
  • 4
  • 51
  • 87