I want to know the difference between
const int* ptr;
and
int * const ptr;
and how it works.
It is pretty difficult for me to understand or keep remember this. Please help.
In the constant pointers to constants, the data pointed to by the pointer is constant and cannot be changed. The pointer itself is constant and cannot change and point somewhere else.
As the name itself indicates, the value of the variable to which the pointer is pointing, is constant. In other words, a pointer through which one cannot change the value of the variable to which it points is known as a pointer to constant.
A pointer to constant is defined as : const <type of pointer>* <name of pointer> An example of definition could be : const int* ptr; Lets take a small code to illustrate a pointer to a constant : #include<stdio.h> int main(void) { int var1 = 0; const int* ptr = &var1; *ptr = 1; printf("%d\n", *ptr); return 0; }
A pointer declaration names a pointer variable and specifies the type of the object to which the variable points. A variable declared as a pointer holds a memory address.
A constant pointer is declared as : int *const ptr ( the location of 'const' make the pointer 'ptr' as constant pointer) 2) Pointer to Constant : These type of pointers are the one which cannot change the value they are pointing to. This means they cannot change the value of the variable whose address they are holding.
means that the pointed data is constant and immutable but the pointer is not. means that the pointer is constant and immutable but the pointed data is not. a pointer to const doesn't say anything about whether the object to which the pointer points is const. Defining a pointer as a pointer to const affects only what we can do with the pointer.
It does not allows modification of its value, however you can modify the value pointed by a pointer. In other words, constant pointer is a pointer that can only point to single object throughout the program. Note: You must initialize a constant pointer at the time of its declaration. Note: We use const keyword to declare a constant pointer.
You can also point to a variable value through it. It is just that you will never be able to modify the value pointed by ptr. Even if it's a duplicate, this answer is by far the best... @JinKwon Of course you can do but it will have an entirely different meaning. @JinKwon const int * const a declares a as const pointer to a const int.
const int* ptr;
declares ptr
a pointer to const int
type. You can modify ptr
itself but the object pointed to by ptr
shall not be modified.
const int a = 10; const int* ptr = &a; *ptr = 5; // wrong ptr++; // right
While
int * const ptr;
declares ptr
a const
pointer to int
type. You are not allowed to modify ptr
but the object pointed to by ptr
can be modified.
int a = 10; int *const ptr = &a; *ptr = 5; // right ptr++; // wrong
Generally I would prefer the declaration like this which make it easy to read and understand (read from right to left):
int const *ptr; // ptr is a pointer to constant int int *const ptr; // ptr is a constant pointer to int
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With