Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 842
embedded_guy Avatar asked Feb 23 '12 18:02

embedded_guy


2 Answers

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.

like image 85
ouah Avatar answered Sep 21 '22 17:09

ouah


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

like image 26
user1229139 Avatar answered Sep 22 '22 17:09

user1229139