Constant and pointer

Discussion in 'C' started by shabbir, Oct 9, 2006.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

    Introduction



    In C you can effectively change the value of the constant variable. Just compile the following program and you will see the output.

    Code:
    void main()
    {
    	int const i=123;
    	int *ip;
    	
    	/* Changing constant integer i.e. const int/int const */
    	
    	printf(" %d %x \n",i,&i);
    	ip=&i;
    	*ip=456;
    	printf(" %d %x %x %d \n",i,&i,ip,*ip);
    	
    }
    Remember to name the file as <filename>.c and not <filename>.cpp You can also download the attachment.

    But when you put the same code in the cpp file the compiler would give you an error message. I am talking with respect to MS compiler.

    Getting into the constants and pointer



    This was about the introduction and how .c and .cpp compiler treated the const. Now I would like to go deep into what does const means in cpp and what are the combination you can use with the object pointer and how the complicated the situation can become.

    const int *p
    int const *p
    int * const p
    const * int p // Warning
    * const int p // Error
    * int const p // Error

    Lets take each one of them and see what it means how it behaves

    const int *p & int const *p



    Read it as "Pointer to constant int". For how to read it refer to the last section of the article, How to read. Now the expression means its a pointer pointing to the constant value. As an example
    Code:
    cout<<"Pointer to constant int"<<endl;
    {
    	int i = 123;
    	const int * p = &i;
    	cout <<"p = i = "<<*p<<endl;
    	p = &i;
    //	*p = 321; [URL=http://www.go4expert.com/articles/difference-constant-pointers-pointer-t26816/]Pointer to constant[/URL] objects so cant change values
    	cout <<"p = i = "<<*p<<endl;
    }
    cout<<"Pointer to constant int"<<endl;
    {
    	int i = 123;
    	int const * p = &i;
    	cout <<"p = i = "<<*p<<endl;
    	p = &i;
    //	*p = 321; Pointer to constant objects so cant change values
    	cout <<"p = i = "<<*p<<endl;
    }
    

    int * const p



    Read it as "Constant pointer". For how to read it refer to the last section of the article, How to read. Now the expression means that the pointer is constant
    Code:
    cout<<"Constant pointer"<<endl;
    {
    	int i = 123;
    	int * const p = &i;
    	cout <<"p = i = "<<*p<<endl;
    //	p = &i; Constant Pointer so unable to change
    	*p = 321;
    	cout <<"p = i = "<<*p<<endl;
    }
    

    const * int p



    This is also not a valid expression and would generate a warning and should be avoided.

    Code:
    cout<<"Pointer to constant int"<<endl;
    {
    	int i = 123;
    	const * int p = &i; 
    	// warning: 'int ' storage-class or type specifier(s) unexpected here; ignored
    	cout <<"p = i = "<<*p<<endl;
    	p = &i;
    //	*p = 321; Pointer to constant objects so cant change values
    	cout <<"p = i = "<<*p<<endl;
    }
    

    * const int p & * int const p



    Declaring a variable in this manner will generate and error.

    How to Read



    You have to read pointer declarations right-to-left.

    const int * p means "p points to an int that is const"
    int * const p means "p is a const pointer to an int"
     

    Attached Files:

  2. aisha.ansari84

    aisha.ansari84 New Member

    Joined:
    Feb 13, 2008
    Messages:
    82
    Likes Received:
    1
    Trophy Points:
    0
    i m really confused with such sorts ,but your article gave some concept
     
  3. peterdrew

    peterdrew New Member

    Joined:
    Jul 21, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    It's very good; thanks!
     
  4. fashionbop

    fashionbop New Member

    Joined:
    Sep 11, 2009
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Shopping from China
    Location:
    Guangzhou, China
    Home Page:
    http://www.fashion-bop.com
  5. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    Really cool, shabbir.

    THX for this
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    The pleasure is mine.
     
  7. qingqing

    qingqing Banned

    Joined:
    Jul 28, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for.
     
  8. gurumanoh

    gurumanoh New Member

    Joined:
    Aug 5, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    good work...
     
  9. wdliming

    wdliming New Member

    Joined:
    Sep 26, 2010
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    good!! I like it!!
     
  10. kien_vn

    kien_vn New Member

    Joined:
    Aug 31, 2010
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    It's very clear to understand - "What is pointer"
     
  11. kumardashish

    kumardashish New Member

    Joined:
    Nov 8, 2008
    Messages:
    17
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    employee
    Location:
    Bangalore
    hi..
    your first program gives a warning because you r trying to store the address of const int variable into int type pointer.
    here is the warning::" different 'const' qualifiers"
    this is the reason you can change the value of const variable.if you create const type pointer variable then it gives an error : l-value specifier...
    try this code..
    Code:
    void main()
    {
        int const i=123;
        int *const ip;
        
        /* Changing constant integer i.e. const int/int const */
        
        printf(" %d %x \n",i,&i);
        ip=&i;
        *ip=456;
        printf(" %d %x %x %d \n",i,&i,ip,*ip);
        
    }
     
    Last edited: Oct 21, 2010
  12. itharsh_04

    itharsh_04 New Member

    Joined:
    Oct 23, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Trainee Wesite Developer
    Location:
    Kutch

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice