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

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; // Passed to function elsewhere. static MySpecialStructure * p_my_special_structure; // Passed to function elsewhere. int myFunction (MySpecialStructure * p_structure, unsigned char useful_char) { ... } 

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; // 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 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?

+4
source share
6 answers

What are the benefits of declaring a pointer as 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?

I assumed that you pointed to a pointer to const .

According to the pointer to const as a parameter, the advantage is that you document the API, telling the programmer that your function does not change the object indicated by the pointer.

For example, see the memcpy prototype:

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

It tells the programmer that the object pointed to by s2 will not be changed when memcpy called.

It also provides documentation related to the compiler, as the implementation will issue diagnostics if you change the pointer to a pointer to const .

+7
source

If you declare the formal parameter as const , the compiler can verify that your code is not trying to change this parameter, which gives the best quality software.

+3
source

const also allows you to tell users of your function that you will not change this parameter behind them.

+3
source

For your first question, if you are damn sure that you are not changing either the pointer or the variable that it points to, you can certainly go and make both of them permanent!

Now for your Qn about why to declare a formal pointer parameter as const, even if the passed pointer is not a constant, a typical use case is the printf() library function. printf should accept const char * , but the compiler does not complain, even if you pass char* . In this case, it makes sense that printf() not based on a user error and inadvertently changes user data! It, like printf() , clearly says: whether you skip const char * or char* , do not worry, I still will not change your data!

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

+2
source

Correctness is a wonderful thing. Firstly, it allows the compiler to help you avoid errors. The obvious simple case is the assignment when you wanted to compare. In this case, if the pointer is const, the compiler will give you an error. Google is "const correctness" and you will find many resources about the benefits of this.

+2
source

Well, what are the benefits of declaring something as const , while you have the option of not doing this? After all, if you donโ€™t touch it, it doesnโ€™t matter if it is const or not. This provides some security checks that the compiler can do for you, and gives some information about the functional interface. For example, you can safely pass a string literal to a function that expects const char * , but you need to be careful if the parameter is declared as a char * .

+1
source

Source: https://habr.com/ru/post/1398098/


All Articles