7

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
  • 1,939
  • 3
  • 24
  • 39

6 Answers6

11

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.

ouah
  • 142,963
  • 15
  • 272
  • 331
4

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

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.

markgz
  • 6,054
  • 1
  • 19
  • 41
2

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

Community
  • 1
  • 1
Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
  • 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 Feb 23 '12 at 18:54
2

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.

Rob K
  • 8,757
  • 2
  • 32
  • 36
1

Well, what are the advantages of declaring anything as a const while you have the option to not to do so? After all, if you don't touch it, it doesn't matter if it's const or not. This provides some safety checks that the compiler can do for you, and it gives some information of the function interface. For example, you can safely pass a string literal to a function that expects a const char *, but you need to be careful if the parameter is declared as just a char *.

sxu
  • 551
  • 4
  • 14