# Pointer and const

Discussion in 'C' started by Rajesh M. Kanojia, Dec 9, 2012.

1. ### Rajesh M. KanojiaNew Member

Joined:
Dec 9, 2012
Messages:
11
0
Trophy Points:
0
Location:
INDIA
Dear members,
can any one tell me what exactly meaning of:-
• Variable constant( const <data type> variable_name);
• pointer to a constant
• constant pointer

2. ### IndiraPNew Member

Joined:
Nov 10, 2012
Messages:
41
2
Trophy Points:
0
constant variable
say
const int a=10;
then u cant change a to any other value... ie.,
a=11; would be an error..

constant pointer

int i=10,j;
int *const a=&i;
then that pointer is a constant pointer to that memory location..so that pointer can't be used to point to other location...
a=&j; //would be an error

pointer to a constant

int i=10;
const int *p=&i;
then the value pointed to by the pointer cant be changed.
*p=11; would be an error..

shabbir likes this.
3. ### Rajesh M. KanojiaNew Member

Joined:
Dec 9, 2012
Messages:
11
0
Trophy Points:
0
Location:
INDIA
dear IndiraP,
i agree with u what u are saying about constant variable, but i get confused when i access constant variable through a pointer, it not only allow me to access the value but through this i can also modify the value of constant variable.

main()
{
const int a=10;
a=11; // generates error
int *ptr;
ptr =&a;
*ptr =11;
printf("\n Now value of constant variable a is : %d",a);
getch();
return 0;
}

plz tell me how variable a become constant.

4. ### IndiraPNew Member

Joined:
Nov 10, 2012
Messages:
41
2
Trophy Points:
0
see...wat do u mean by constant variable first of all???

u r actually making a variable hold a constant value in it instead of allowing it to handle multiple varibale values..rite..???

now in the program u mentioned..u made "a" variable constant..means "a" can only hold 10...so it can't take any other value not even 11..!!!

n when u created a pointer for it...look carefully...u created a pointer to "a" which is constant n which holds only 10 ..rite???

it means.. a pointer to a constant...so wat happens..even the pointer can't change the value that it is pointing to...afterall "a"-->can only hold 10....n when u r manipulating *p..u r actually trying to change "a'"s value ..so will it allow.??? no ...rite??

n "a" is nothing but the name of the memory location where 10 resides...so p=&a..makes p point to the memory location of "a"...n *p is value at &a...where that memory is locked to hold only 10 by specifying it constant...so *p=11 is error..

n refer to pointer to constant..for any better understanding..

got it???

Last edited: Dec 12, 2012
Rajesh M. Kanojia likes this.
5. ### Rajesh M. KanojiaNew Member

Joined:
Dec 9, 2012
Messages:
11
0
Trophy Points:
0
Location:
INDIA
Dear Indira P
I got u and also agree with u but first copy the below program and run then see what i am asking:-
Code:
```#include<stdio.h>
#include<conio.h>
int main()
{
const int a=10;
int *ptr;
ptr=&a;
printf("\n Now the value at memory : %u  is : %d  ",&a,a);
*ptr=11;
printf("\n Now the value at memory : %u  is : %d  ",&a,a);
getch();
return 0 ;
}```

Last edited by a moderator: Dec 13, 2012

Joined:
Nov 10, 2012
Messages:
41