Answers

Question and Answer:

  Home  C Programming

⟩ What is a const pointer in C?

The access modifier keyword const is a promise the programmer makes to the compiler that the value of a variable will not be changed after it is initialized. The compiler will enforce that promise as best it can by not enabling the programmer to write code which modifies a variable that has been declared const.

A const pointer, or more correctly, a pointer to const, is a pointer which points to data that is const (constant, or unchanging). A pointer to const is declared by putting the word const at the beginning of the pointer declaration. This declares a pointer which points to data that can’t be modified. The pointer itself can be modified. The following example illustrates some legal and illegal uses of a const pointer:

const char *str = hello;

char c = *str /* legal */

str++; /* legal */

*str = ‘a’; /* illegal */

str[1] = ‘b’; /* illegal */

 195 views

More Questions for you: