Difference between normal pointer and const pointer in c

Difference between normal pointer and const pointer in c



Actually i don't know what is the difference between a normal pointer and a const pointer
if i use the below code it will works fine . but when i change int *ptr=# to int *const ptr = &var1; then it will not work. can anybody explain what is the difference between a normal pointer and a const pointer ?


int *ptr=#


int *const ptr = &var1;


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;




6 Answers
6


int* const ptr = &num ;



Will create a constant pointer to an int. The data it points to can be changed, but the pointer it self cannot.



You cannot change the pointer:


ptr++ ;



But you can change the data:


*ptr = 1234 ;





can u tell me actually where const pointers are used
– Arunprasanth K V
Nov 4 '14 at 13:07





@ArunPrasanth If you know that the pointer should always point to the same location, you can make it const.
– 2501
Nov 4 '14 at 13:07



We can do following operations on constant pointers



We Can't to following operation on constant pointers



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!



This:


int* const ptr = &num ;



will create a constant pointer to an integer. You can use it to modify the integer's value but you can't change where the pointer points, thus ptr++ ; is invalid.


ptr++ ;



The const keyword is usually applied to its left symbol, e.g.


const


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;)



const pointers are useful when you want to pass a fixed memory location around and you want to make sure that nobody will modify the pointer's pointed address.


const





Thanks for your quick replay
– Arunprasanth K V
Nov 4 '14 at 13:11



in c, const is a type qualifier. use of const in some variable definition means, the variable will not get modified (will be treated as read-only)during the entire lifetime of the program.


const


type qualifier


const


read-only



Usually, when defining a variable / data type with const, the pratice is to initialize it with required value, as normally, the value it holds cannot be modified at a later part.


const



For example:



const int a = 10;


const int a = 10;



means, the integer a will hold the value 10 and it cannot be changed. at a later part,


a


10


a = 20;



will produce error.



So, in your case


int *const ptr = &var;



here, ptr will always hold the address of var and it cannot be changed, i.e., we cannot write


ptr


var


ptr = &num2; // where num2 is another int, declared like int num2;



it will show compile-time error like :



error:assignment of read-only variable "*ptr".



You can find a nice and handy description here.


int* const pointer = &x ;



it create a constant pointer to an int. The data it points to can be changed, but the pointer it self cannot be changed



You cannot change the pointer:


pointer++ ;



here you can change the data:


*pointer=1 ;





In case of normal pointer, both the value of pointer & value @ pointer can be change.But as the pointer change the value @ pointer automatically change to some garbage value.


#include <stdio.h>

int main ()

int val = 5;
int *ptr = (int*)&val;

printf("val@ptr : %d nptr : %xn", *(ptr), (int*)ptr);

val++;
printf("nIncrement val++n");
printf("val@ptr : %d nptr : %xn", *(ptr), (int*)ptr);

ptr++;
printf("nIncrement ptr++n");
printf("val@ptr : %d nptr : %xn", *(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 case of const pointer, only the value @ pointer can be change not the pointer. See the below example.


#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 : %dt and ptr val : %xn", *(ptr), (int*)ptr);
val++;
//ptr++;
printf("The value1 @ptr : %dt and ptr val : %xn", *(ptr), (int*)ptr);

return 0;



Output:


The value1 @ptr : 10 and ptr val : ee2ccf24
The value1 @ptr : 11 and ptr val : ee2ccf24



If we un-comment the line number 11, then output would be like this:


main.c:11:7: error: increment of read-only variable ‘ptr’
ptr++;



Please go through the link to get better understanding : http://c-faq.com/decl/spiral.anderson.html






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)