cpointersconstants

Why is "const char * const" not used as input string argument for a function in C?


Here is a useful reminder about pointers in C:

int       *      mutable_pointer_to_mutable_int;
int const *      mutable_pointer_to_constant_int;
const int *      mutable_pointer_to_constant_int;
int       *const constant_pointer_to_mutable_int;
int const *const constant_pointer_to_constant_int;
const int *const constant_pointer_to_constant_int;

Now, let's say I have a function do_something that takes a string as input. The string will not be modified. I would write something like this:

void do_something(char const* const string);

Here's the thing: when looking at API code provided by microcontroller manufacturers (which is supposed to be well written), in such a case, I rarely see the type char const * const used as an input type. Instead, they typically write char const* string or const char* string.

Is there a specific reason for this? Wouldn't it be cleaner to make the pointer itself const as well?

Thank


Solution

  • Let's look at a simpler case first.

    Given

    void do_something1( int i ) { ... }
    
    void do_something2( const int j ) { ... }
    

    The following applies:

    So,

    Keep in mind that parameters are local to their function, so changes to them have no effect in the caller. So making the parameter const only serves to shackle the writer of the function. The benefits of doing so are elusive to me.


    The same applies in your code.

    Given

    void do_something3( char const *s ) { ... }
    
    void do_something4( char const * const t ) { ... }
    

    The following applies:

    So,

    Again, keep in mind that parameters are local to their function, so changes to them have no effect in the caller. So making the parameter const only serves to shackle the writer of the function. The benefits of doing so are elusive to me.