Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What use are const pointers (as opposed to pointers to const objects)?

Tags:

c++

c

constants

I've often used pointers to const objects, like so...

const int *p; 

That simply means that you can't change the integer that p is pointing at through p. But I've also seen reference to const pointers, declared like this...

int* const p; 

As I understand it, that means that the pointer variable itself is constant -- you can change the integer it points at all day long, but you can't make it point at something else.

What possible use would that have?

like image 359
Head Geek Avatar asked Oct 20 '08 21:10

Head Geek


People also ask

What is the use of const pointer?

A pointer to a const value (sometimes called a pointer to const for short) is a (non-const) pointer that points to a constant value. In the above example, ptr points to a const int . Because the data type being pointed to is const, the value being pointed to can't be changed. We can also make a pointer itself constant.

What are the differences between pointers to constants and constant pointers?

A constant pointer is a pointer that holds the address of only one location during it's entire lifetime whereas a pointer pointing to the location of a constant variable is a pointer to constant.

What is the use of const pointer in C?

A constant pointer in C cannot change the address of the variable to which it is pointing, i.e., the address will remain constant. Therefore, we can say that if a constant pointer is pointing to some variable, then it cannot point to any other variable.

What does a const pointer mean?

A constant pointer is one that cannot change the address it contains. In other words, we can say that once a constant pointer points to a variable, it cannot point to any other variable. Note: However, these pointers can change the value of the variable they point to but cannot change the address they are holding.


2 Answers

When you're designing C programs for embedded systems, or special purpose programs that need to refer to the same memory (multi-processor applications sharing memory) then you need constant pointers.

For instance, I have a 32 bit MIPs processor that has a little LCD attached to it. I have to write my LCD data to a specific port in memory, which then gets sent to the LCD controller.

I could #define that number, but then I also have to cast it as a pointer, and the C compiler doesn't have as many options when I do that.

Further, I might need it to be volatile, which can also be cast, but it's easier and clearer to use the syntax provided - a const pointer to a volatile memory location.

For PC programs, an example would be: If you design DOS VGA games (there are tutorials online which are fun to go through to learn basic low level graphics) then you need to write to the VGA memory, which might be referenced as an offset from a const pointer.

-Adam

like image 54
Adam Davis Avatar answered Oct 12 '22 03:10

Adam Davis


It allows you to protect the pointer from being changed. This means you can protect assumptions you make based on the pointer never changing or from unintentional modification, for example:

int* const p = &i;  ...  p++;     /* Compiler error, oops you meant */ (*p)++;  /* Increment the number */ 
like image 21
Andrew Johnson Avatar answered Oct 12 '22 03:10

Andrew Johnson