answersLogoWhite

0

What is a pointer constant?

Updated: 10/21/2022
User Avatar

Wiki User

10y ago

Best Answer

In programming, a pointer is a variable that stores a memory address. We say that the variable 'points at' the memory address, because we can access that memory indirectly via the pointer variable. And since they are variable, we can change the address stored in the pointer at any time.

A constant pointer, on the other hand, is a pointer that is guaranteed to always point at the same memory address it was given when it was first instantiated. That memory address cannot be altered until the pointer falls from scope, at which point it can be re-instantiated with a different memory address. But while it is in scope, it cannot be altered.

Note that a constant pointer is not the same as a pointer to a constant. A pointer to a constant is still a variable, only the memory being pointed at is regarded as being constant. That is, we can change the memory address stored in the pointer, but we cannot change what is stored in that memory address.

To better understand the difference, consider the following declarations in C++ code:

int x[2] = { 1, 2 };

int* p1 = &x[0];

const int* p2 = &x[0];

int* const p3 = &x[0];

const int* const p4 = &x[0];

Here, p1, p2, p3 and p4 are all declared as pointers to integers while x is an integer array with two elements. x[0] is the first element, with value 1, while x[1] is the second element, with value 2. All four pointers initially point at the address occupied by x[0] and all can indirectly access the value contained therein.

p1 is declared as a pointer to an integer. Neither the pointer nor the integer are declared constant, so this means we can use pointer arithmetic on the pointer, incrementing it so it points at x[1], and we can also alter the value of the integer contained therein via the indirection operator:

p1++; // point to x[1]

(*p1)++; // indirectly increment the value of x[1]; so 2 becomes 3

p2 is declared as a pointer to a constant integer so we can adjust the pointer, but we cannot alter the value being pointed at:

p2++; // point to x[1]

(*p2)++; // illegal; x[1] still has the value 3!

p3 is declared as a constant pointer to an integer, so we cannot adjust the pointer but we can adjust the value being pointed at:

p3++; // illegal; still points to x[0]!

(*p3)++; // indirectly increment the value of x[0], so 1 is now 2

p4 is declared as a constant pointer to a constant integer, so we cannot adjust either the pointer or the value being pointed at:

p4++; // illegal; still points to x[0]!

(*p4)++; // illegal; x[0] still has the value 2!

In other words, p1 and p2 are pointer variables, while p3 and p4 are both constant pointers. Note that you will quite often hear people calling p2 a constant pointer but it is actually a pointer to a constant. In that variation it is the memory being pointed at that is regarded as being the constant, not the pointer itself.

Of course the illegal statements would not be permitted in legitimate code -- the compiler will warn us that the variables we are attempting to modify are in fact qualified as being constant and therefore cannot be modified. However, this is the one and only reason we use constants; to alert us to inadvertent bugs in our code. When we declare a constant, we are enlisting the aid of the compiler to assure us that we do not inadvertently alter variables that should not be altered. The more bugs we can catch with the compiler's help, the better. If only runtime bugs could be resolved as easily.

User Avatar

Claudine Hermann

Lvl 9
1y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a pointer constant?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Difference between pointer to constant and constant pointer?

1. pointer to a constant means you can not change what the pointer points to 2. constant pointer means you can not change the pointer.


What is the difference between the Constant pointer and pointer constant and explain it with an example?

Pointer to constant *ptr=10 statement is invalid in pointer to constant i.e assigning value is illegal ptr++ statement is valid in pointer to constant. pointer can be incremented and decremented, Pointer is pointing to constant data object. Declaration: const int *ptr; Constant pointers: *ptr= 10 is absolutely valid in constant pointers i.e assigning value is perfectly legal ptr+++ statement is invalid in constant pointers. pointer can not be incremented or decremented. Declaration; int *const ptr;


Which two pointer does not increment or decrement in arithmetic array?

constant pointer and character pointer


Why can't we increment an array like a pointer?

once we initialize the array variable, the pointer points base address only & it's fixed and constant pointer


WHAT IS POINTER TO POINTER IN C POINTER?

Pointer in C is Memory Reference. It stores memory address of any variable, constant, function or something you later use in your programming. Pointer basically used to ease the referencing of variables and others or in polymorphism and inheritance.


What is the time taken to insert an element after an element pointed by some pointer?

Constant time.


How can you change values in call by reference?

We don't call by reference, we call functions. The arguments passed to the function are passed (not called) either by value or by reference, depending upon the function signature (the prototype). When you pass by reference you are passing the actual variable, not a copy of the variable, thus the function can modify that variable's value directly. The only exception is when the parameter is declared a constant reference. Passing a pointer is essentially the same as passing by reference, however the pointer itself is passed by value. To pass a pointer by reference you must pass a pointer-to-pointer instead. Passing by value always copies the value, whether it is declared constant or not. But if it is declared constant, the function might as well accept a constant reference. Passing objects (instances of a class) by constant value will incur a performance penalty in making an unnecessary copy. If it is constant, there is little point in copying the object.


Why pointer can't be added in computer language C?

You can add a point in C/C++. The legal operations on a pointer are that you can 1.) add a constant, 2.) subtract a constant, and 3.) subtract two pointers that refer to the same array. Anything else is meaningless.


What does it mean for consistant burning in breasts?

A constant burning sensation in breasts could be a pointer to breast cancer.


An array name is a pointer constant?

An array's name is not a constant pointer; it's not even a pointer! An array name is a reference to the array itself. Unlike pointers, references have no memory of their own, therefore a reference cannot be constant (only what it refers to can be constant). A reference is nothing more than an alias for a memory address. Since there is no separate storage for references, you cannot reassign references while they remain in scope. So, in that sense, it behaves like a constant pointer. But a pointer is a variable, so even if declared const, it requires memory of its own in order to store the memory address it points to.Example:int a[10];int * const p = a; // const pointerassert( *p &a ); would cause an assertion. Likewise, assert( p != &p); proves that p must reside in a separate memory address from that referred to by a. It has to: pointers are variables even when they are declared const.


How would you read int const p?

int const *p declares a 'p' pointer, that points to a constant integer


What is an alias name given to a variable in opp with c plus plus?

An alias is a reference, an alternate name for a variable or constant. You can assign the address of any variable or constant to a reference of the same type. A reference is a bit like a constant pointer to the type but, unlike a pointer, a reference has no address of its own thus you cannot store references. More importantly, references can never be NULL. They are simply an alternative name by which you can refer to an existing variable or constant. When you assign a value to an existing reference to a variable, you are assigning the value to the variable itself. When you pass a reference to a function, you are passing the address of the value being referred to, and that address is assigned to the function's reference argument and is local to the function. This is not unlike passing a pointer, but pointers may be NULL, references are guaranteed to be non-NULL (a NULL reference invalidates your program). Note that C++ references are not the same as C reference variables or constants. In C, a reference variable is simply a non-const pointer, while a reference constant is a constant pointer. Hence pointers can be dereferenced (both in C and C++). But in C++, a reference is neither a variable nor a pointer, but is constant (it always refers to the same object and cannot be reassigned once assigned).