1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Pointer and const

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

  1. Rajesh M. Kanojia

    Rajesh M. Kanojia New Member

    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. IndiraP

    IndiraP New Member

    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..
    shabbir likes this.
  3. Rajesh M. Kanojia

    Rajesh M. Kanojia New Member

    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.
  4. IndiraP

    IndiraP New Member

    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. Kanojia

    Rajesh M. Kanojia New Member

    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 ;
    Last edited by a moderator: Dec 13, 2012
  6. IndiraP

    IndiraP New Member

    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..!!!

Share This Page