c++ - Practical use of pointer to const -


i understand use of pointer constant strlen implementation.

size_t strlen ( const char * str ); 

can suggest other reasons or provide scenarios 'pointer const value' useful in practice.

think of way. want me @ value of variable don't want me alter variable in way, pass me constant. when use function , see parameter constant know there contract between , says should not change value of variable nor can directly.

when write code don't know use functions. practice protect code. protects yourself, compiler errors moment tr change value of variable.

a side note: true in c can still change value though parameter says const, take pointer alter content of variable in memory.

try compiling code , notice how compiler protects making mistake.

const char *cookies(const char *s) {    return ('\0' == *s)? s: s + 1; } 

it won't let compile, why? because trying change const variable.

another post same question here: const usage pointers in c


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -