Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Pointer and const (http://www.go4expert.com/forums/pointer-and-const-t29334/)

Rajesh M. Kanojia 9Dec2012 21:15

Pointer and const
Dear members,
can any one tell me what exactly meaning of:-
  • Variable constant( const <data type> variable_name);
  • pointer to a constant
  • constant pointer

IndiraP 12Dec2012 02:36

Re: Pointer and const
constant variable
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..

Rajesh M. Kanojia 12Dec2012 22:00

Re: Pointer and const
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.

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

plz tell me how variable a become constant.

IndiraP 12Dec2012 22:18

Re: Pointer and const
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???

Rajesh M. Kanojia 13Dec2012 14:27

Re: Pointer and const
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:-

int main()
        const int a=10;
        int *ptr;
        printf("\n Now the value at memory : %u  is : %d  ",&a,a);       
        printf("\n Now the value at memory : %u  is : %d  ",&a,a);
        return 0 ;

IndiraP 13Dec2012 15:09

Re: Pointer and const
yeah..i did run it..n u get error at p=&a; stating that "const int * can't be converted into int * "
it's because...tha variable is constant...so the pointer that it points to becomes pointer to a constant..rite???

but *p is declared as normal integer...so in order to eliminate the error make *p a const int *p;

and coming to *p=11...u can't change the value anyway..!!!

All times are GMT +5.5. The time now is 17:39.