Pointer to constant vs. pointer constant - C++

Pointer to constant vs. pointer constant.

- Pointer to constant points to a value that does not change and is declared as :
const type * name
type is data type
name is name of the pointer
Example :
const char *p;
- Pointer to constant can not be used to change the value being pointed to. Therefore :
char ch = ‘A’;
const char *p = &ch;
*p = ‘B’;
is not allowed. The program will throw an error.
Pointer Constant (or constant pointer):
- It is a pointer which you don’t want to be pointed to a different value. That is, the location stored in the pointer can not change. We can not change where the pointer points.
- It is declared as :
type * const name
type is data type
name is name of the pointer
Example :
char * const p
- Since the location to which a const pointer points to can not be changed, the following code :
char ch1 = ‘A’;
char ch2 = ‘B’;
char * const p = &ch1;
p = &ch2;
will throw an error since address stored in p can not be changed.
What are function pointers?
What are function pointers? - A function has a physical location in the memory which is the entry point of the function...
What is pointer to member? - C++
What is pointer to member? - This type of pointer is called a pointer to a class member or a pointer-to-member....
What is const pointer and const reference? - C++
What is const pointer and const reference? - const pointer is a pointer which you don’t want to be pointed to a different value.....
Post your comment