Declaring a pointer to const or a const pointer to const as a formal parameter

15,089

Solution 1

What are the advantages of declaring a pointer as a const - as a formal parameter? I know that I can do it, but why would I want to... particularly in the case where the pointer being passed and the data it is pointing to are not declared constant?

I assumed you meant a pointer to const.

By have a pointer to const as a parameter, the advantage is you document the API by telling the programmer your function does not modify the object pointed by the pointer.

For example look at memcpy prototype:

void *memcpy(void * restrict s1, const void * restrict s2, size_t n);

It tells the programmer the object pointed to by s2 will not be modified through memcpy call.

It also provides compiler enforced documentation as the implementation will issue a diagnostic if you modify a pointee from a pointer to const.

Solution 2

const also allows to indicate users of your function that you won't modify this parameter behind their back

Solution 3

If you declare a formal parameter as const, the compiler can check that your code does not attempt to modify that parameter, yielding better software quality.

Solution 4

For your first question, if you are damn sure of not modifying either the pointer or the variable it points to, you can by all means go ahead and make both of them constant!

Now, for your Qn as to why declare a formal pointer parameter as const even though the passed pointer is not constant, A typical use case is library function printf(). printf is supposed to accept const char * but the compiler doesn't complain even if you pass a char* to it. In such a case, it makes sense that printf() doesn't not build upon the user's mistake and alter user's data inadvertantly! Its like printf() clearly telling- Whether you pass a const char * or char*, dont worry, I still wont modify your data!

For your second question, const pointers find excellent application in the embedded world where we generally write to a memory address directly. Here is the detailed explanation

Solution 5

Const correctness is a wonderful thing. For one, it lets the compiler help keep you from making mistakes. An obvious simple case is assigning when you meant to compare. In that instance, if the pointer is const, the compiler will give you an error. Google 'const correctness' and you'll find many resources on the benefits of it.

Share:
15,089
embedded_guy
Author by

embedded_guy

Electrical and Embedded Systems Design Engineer

Updated on June 08, 2022

Comments

  • embedded_guy
    embedded_guy almost 2 years

    I was recently making some adjustments to code wherein I had to change a formal parameter in a function. Originally, the parameter was similar to the following (note, the structure was typedef'd earlier):

    static MySpecialStructure my_special_structure;
    static unsigned char char_being_passed;              // Passed to function elsewhere.
    static MySpecialStructure * p_my_special_structure;  // Passed to function elsewhere.
    
    int myFunction (MySpecialStructure * p_structure, unsigned char useful_char)
    {
        ...
    }
    

    The change was made because I could define and initialize my_special_structure before compile time and myFunction never changed the value of it. This led to the following change:

    static const MySpecialStructure my_special_structure;
    static unsigned char char_being_passed;              // Passed to function elsewhere.
    static MySpecialStructure * p_my_special_structure;  // Passed to function elsewhere.
    
    int myFunction (const MySpecialStructure * p_structure, unsigned char useful_char)
    {
        ...
    }
    

    I also noticed that when I ran Lint on my program that there were several Info 818's referencing a number of different functions. The info stated that "Pointer parameter 'x' (line 253) could be declared as pointing to const".

    Now, I have two questions in regards to the above. First, in regards to the above code, since neither the pointer nor the variables within MySpecialStructure is changed within the function, is it beneficial to declare the pointer as constant as well? e.g. -

    int myFunction (const MySpecialStructure * const p_structure, unsigned char useful_char)
    

    My second question is in regards to the Lint information. Are there any benefits or drawbacks to declaring pointers as a constant formal parameter if the function is not changing its value... even if what you are passing to the function is never declared as a constant? e.g. -

    static unsigned char my_char;
    static unsigned char * p_my_char;
    p_my_char = &my_char;
    
    int myFunction (const unsigned char * p_char)
    {
        ...
    }
    

    Thanks for your help!

    Edited for clarification -

    What are the advantages of declaring a pointer to const or a const pointer to const- as a formal parameter? I know that I can do it, but why would I want to... particularly in the case where the pointer being passed and the data it is pointing to are not declared constant?

  • embedded_guy
    embedded_guy about 12 years
    I think I should have worded my question a little better... what are the advantages of declaring a pointer as a const as a formal parameter? I know that I can do it, but why would I want to... particularly in the case where the pointer being passed and the data it is pointing to are not declared constant?
  • embedded_guy
    embedded_guy about 12 years
    I did mean a pointer to const, thanks... I hate it when I try to clarify only to add more confusion.