Recently, I made adjustments to the code in which I had to change the formal parameter in the function. Initially, the parameter was similar to the following (note that the structure was previously typedef'd):
static MySpecialStructure my_special_structure; static unsigned char char_being_passed;
This change was made because I could define and initialize my_special_structure until compilation time, and myFunction never changed its value. This led to the following change:
static const MySpecialStructure my_special_structure; static unsigned char char_being_passed;
I also noticed that when I started Lint in my program, there were several Info 818 references to a number of different functions. The information indicates that the 'Pointer' x ' parameter (line 253) can be declared as pointing to const .
Now I have two questions regarding the foregoing. Firstly, with regard to the above code, since neither the pointer nor the variables in MySpecialStructure change inside the function, is it useful to declare the pointer as constant? eg -
int myFunction (const MySpecialStructure * const p_structure, unsigned char useful_char)
My second question is about Lint information. Are there any advantages or disadvantages to declaring pointers as a constant formal parameter if the function does not change its value ... even if what you pass to the function is never declared as a constant? eg -
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 to clarify -
What are the advantages of declaring a pointer to a const or const pointer to const - as a formal parameter ? I know I can do this, but why do I want ... especially when the passed pointer and the data it points to are not declared constant?
source share