When should the const modifier be used? Ans. There are several reasons to use const pointers. First, it allows the compiler to catch errors in which code accidentally changes the value of a variable, as in while (*str = 0) /* programmer meant to write *str != 0 */ { /* some code here */ str ; } in which the = sign is a typographical error. Without the const in the declaration of str, the program would compile but not run properly. Another reason is efficiency. The compiler might be able to make certain optimizations to the code generated if it knows that a variable will not be changed. Any function parameter which points to data that is not modified by the function or by any function it calls should declare the pointer a pointer to const. Function parameters that are passed by value (rather than through a pointer) can be declared const if neither the function nor any function it calls modifies the data. In practice, however, such parameters are usually declared const only if it might be more efficient for the compiler to access the data through a pointer than by copying it. - Study24x7
Social learning Network
17 Mar 2023 11:16 AM study24x7 study24x7

When should the const modifier be used? Ans. There are several reasons to use const pointers. First, it allows the compiler to catch errors in which code accidentally changes the value of a variable, as in while (*str = 0) /* programmer meant to write *str != 0 */ { /* some...

See more

study24x7
Write a comment
Related Questions
500+   more Questions to answer
Most Related Articles