Pointers to Constants - C Pointer

C examples for Pointer:Constant Pointer

Introduction

You can use the const keyword when you declare a pointer to indicate that the value pointed to must not be changed.

Here's an example of a declaration of a pointer to a const value:

long value = 9999L;
const long *pvalue = &value;               // Defines a pointer to a constant

For example, the following statement will now result in an error message from the compiler:

*pvalue = 8888L;                  // Error - attempt to change const location

The pointer itself is not constant, so you can still change what it points to:

long number = 8888L;
pvalue = &number;                 // OK - changing the address in pvalue

Related Tutorials