3

Let's consider such application:

void foo (char* const constPointerToChar) {
    // compile-time error: you cannot assign to a variable that is const
    constPointerToChar = "foo";
}

int _tmain(int argc, _TCHAR* argv[])
{
    char* str = "hello";
    foo(str);
    printf(str);
    return 0;
}

Let's remove const keyword:

void foo (char* pointerToChar) {
    pointerToChar = "foo";
}

int _tmain(int argc, _TCHAR* argv[])
{
    char* str = "hello";
    foo(str);
    printf(str);
    return 0;
}

And output is hello. So even if function is allowed to change pointer it changes it's copy of pointer and original pointer was not changed.

This is expected because pointers are passed by value.

I do understand why things works this way but I do not understand why someone need to declare parameter as X* const.

When we declare function parameter as X* const we say that "Ok I promise inside my function i will not modify my own copy of your pointer." But why caller should care what happens with variables he never see and use?

Am I correct that declaring function parameter as X* const is useless?

Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
  • 1
    There's no good reason (in my opinion) to declare a parameter in that way. So yes, it's 'pointless', and you summed it up pretty well. But, you know, this is a style question so it's practically certain that someone will tell me I'm completely wrong. – john Apr 16 '13 at 16:57
  • Has been asked quite a few times. Possible duplicate of [Use of 'const' for function parameters](http://stackoverflow.com/questions/117293/use-of-const-for-function-parameters) – AnT stands with Russia Apr 16 '13 at 16:58
  • 1
    Also http://stackoverflow.com/questions/5385754/is-it-true-that-const-t-v-is-never-necessary-in-c – AnT stands with Russia Apr 16 '13 at 16:59

3 Answers3

9

But why caller should care what happens with variables he never see and use?

He doesn't. In fact, you're allowed to leave that const out of declarations of the function, and only include it in the implementation.

Am I correct that declaring function parameter as X* const is useless?

No, it's as useful as declaring any other local variable const. Someone reading the function will know that the pointer value shouldn't change, which can make the logic a bit easier to follow; and no-one can accidentally break the logic by changing it when they shouldn't.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • 6
    In addition, the compiler should warn you if you, by mistake, write a statement that alters the parameter. So it is a bug-prevention technique. – Eric Postpischil Apr 16 '13 at 17:02
  • And since it *doesn't* affect calling code, C++ also allows that `const` to be omitted from the *declaration* and only be specified in the *definition*. – Drew Dormann Apr 16 '13 at 17:11
2

The top level qualifier will be discarded in the function declaration (so it does not take part in the function signature) but will be enforced in the function definition.

There are some coding standards that suggest that you should take the value arguments as const (and return also const values), as to avoid potentially unwanted modifications of the arguments. I don't quite agree with that rationale, but there it is.

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
0

const has 2 general effects:

  • It forces the programmer to adhere to the standards. Since you cannot pass a non-const variable into a const parameter without declaring it mutable, the programmer calling the function knows his variable will not be changed by the function.
  • And more importantly - the compiler can optimize based on this information, and internally save some copies, or do inline expansions, or whatever, that wouldn't be possible without the prior knowledge that the called function will NEVER change the data because it simply can't.
Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136