Difference between normal pointer and const pointer in c
I don't know the difference between a normal pointer and a pointer const
. The code below works fine, but when I change int *ptr=#
to int *const ptr = &var1;
then it is not. What is the difference between a normal pointer and a pointer const
?
int main(void)
{
int num = 20;
int *ptr = &num ; // if i change to 'int *const ptr = &var1;' then it shows some error
*ptr = 20 ; // Valid
ptr ++ ; // valid
return 0;
}
source to share
We can do the following operations with persistent pointers
- Assigning a value by address
- Print value or address
- Address assignment during announcement.
We cannot perform the following operation with constant pointers
- Adding integers to constant pointers.
- Subtraction of integers to constant pointers.
- Any operation that can change the address of a pointer.
So here in your question ..
If you declare
int* const ptr = &num ; // this is ok
next line
*ptr = 20 ; // Assigning value at address this is ok
Now
ptr ++ ; // you can not change the value // Error!
Hope it helps!
source to share
It:
int* const ptr = &num ;
will create a constant pointer for an integer. You can use it to change an integer value, but you cannot change where the pointer points to, so it is ptr++ ;
not valid.
The keyword is const
usually applied to its left character, eg.
int * const ptr; // A constant pointer (*)
int const * ptr; // A pointer to a constant integer
int const * const ptr; // A constant pointer to a constant integer
const int *ptr; // Shorthand for pointer to a constant integer (equivalent to int const * ptr;)
Pointers const
useful when you want to transfer a fixed memory location and you want to make sure no one changes the pointer address.
source to share
in c, const
is type qualifier
. use const
in some means of defining a variable, the variable will not be changed (will be processed as read-only
) for the entire duration of the program.
Usually, when defining a variable / datatype with const
, pratice should initialize it with the required value, as usual, the value it holds cannot be changed in a later part.
For example:
const int a = 10;
means that the integer a
will contain the value 10
and cannot be changed. in the later part
a = 20;
will result in an error.
So in your case
int *const ptr = &var;
there ptr
will always contain the address var
and can not be changed, ie, we can not write
ptr = &num2; // where num2 is another int, declared like int num2;
a compile time error will be displayed like:
error: assigns a read-only variable "* ptr".
You can find a nice and handy description here .
source to share
In the case of a conventional pointer both values pointer & meaning @ pointer can be changed. But when the pointer changes , the @ pointer value will automatically change to some garbage value.
#include <stdio.h>
int main () {
int val = 5;
int *ptr = (int*)&val;
printf("val@ptr : %d \nptr : %x\n", *(ptr), (int*)ptr);
val++;
printf("\nIncrement val++\n");
printf("val@ptr : %d \nptr : %x\n", *(ptr), (int*)ptr);
ptr++;
printf("\nIncrement ptr++\n");
printf("val@ptr : %d \nptr : %x\n", *(ptr), (int*)ptr);
return 0;
}
Output:
val@ptr : 5
ptr : 93ddc274
Increment val++
val@ptr : 6
ptr : 93ddc274
Increment ptr++
val@ptr : -1814183304
ptr : 93ddc278
But in the case of a constant pointer, you can only change the @ pointer value , not the pointer. See an example below.
#include <stdio.h>
int main () {
int val = 10;
//always start reading from right to left
int *const ptr = (int*)&val;//ptr is const pointer to int, i.e. ptr can not be change at all.
printf("The value1 @ptr : %d\t and ptr val : %x\n", *(ptr), (int*)ptr);
val++;
//ptr++;
printf("The value1 @ptr : %d\t and ptr val : %x\n", *(ptr), (int*)ptr);
return 0;
}
Output:
The value1 @ptr : 10 and ptr val : ee2ccf24
The value1 @ptr : 11 and ptr val : ee2ccf24
If we comment out line 11, the result is:
main.c:11:7: error: increment of read-only variable ‘ptr
ptr++;
Please follow the link to understand better: http://c-faq.com/decl/spiral.anderson.html
source to share