2

I have seen code where people will use const as a parameter to the function. What are the benefits of using const* vs const * const? This might be a very basic question but I would appreciate if someone could explain.

Bool IsThisNumberEqualToFive(int const * num)
{
    return(Bool)(5 != num );
}

Bool IsThisNumberEqualToFive(int const * const num)
{
    return(Bool)(5 != num );
}
user968000
  • 1,765
  • 3
  • 22
  • 31
  • 1
    Somewhat related: You may find [this answer to a previous question](http://stackoverflow.com/questions/14562845/why-does-passing-char-as-const-char-generate-a-warning/14566215#14566215) interesting. not directly related to your question, so not a dupe, but check out the pointer-type list and the effects of `const`. – WhozCraig Jan 13 '14 at 17:39

3 Answers3

6
  • In the first version you promise you're not going to write to the object that num points to
  • In the second version you promise that and you also prevent yourself (i.e. IsThisNumber..) from making num point to something else.

That is, in the second version the pointer itself is also const, in addition to the pointee.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • So In a function like where we just have to check whether a statement is true or not (We are not changing anything inside the function)we should use the second option ? – user968000 Jan 13 '14 at 17:40
  • Technically it allows the compiler to optimize more. In practice you care more about the pointee-level const. – cnicutar Jan 13 '14 at 17:42
  • 1
    @user968000 Personally I consider top level `const`s like the one in the second function pretty much worthless. Someone calling that function doesn't care whether you do `++num;` within the function, but they're more likely to care that you don't do `*num = 42;`, which the first version promises you won't. – Praetorian Jan 13 '14 at 17:44
  • 1
    @user968000 Your code should also reflect the dereference of `*num`, which it currently does not. As written, unless your pointer contains an address `0x00000005` (highly unlikely), the function will always return `true`. I believe you wanted to use `*num` in those evals. – WhozCraig Jan 13 '14 at 17:45
  • @WhozCraig Good call! – cnicutar Jan 13 '14 at 17:45
  • @cnicutar Heh. not your problem, your answer is *solid*. Just there for the OP. – WhozCraig Jan 13 '14 at 17:45
2

For the parameter

int const * num  // num is a pointer to const int. 

const is protecting the num points to.The program can alter the value of num but not *num. While for

int const * const num  // num is a const pointer to const int

leftmost const is protecting the num points to while right most is protecting pointer num it self. In this case neither num nor *num is going to modify.

NOTE: Understanding what does

T const *p;  // T is any valid type

means (more precisely).
This means that, a program can use the expression p to alter the value of the pointer object that p designates, but it can’t use the expression *p to alter the value of any objects that *p might designate. If the program has another expression e of unqualified type that designates an object that *p also designates,the program can still use e to change that object.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • @ViktorLexington; In first case pointer can be changed but not the value pointed by the pointer. For second example you are right. – haccks Jan 13 '14 at 17:40
1

The more the sources tell the compiler on what will happend to variables' values (or better "not happen" as it would be expected for const variables) in advance the better the compiler can optmise.

alk
  • 69,737
  • 10
  • 105
  • 255