Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Constant and pointer (http://www.go4expert.com/articles/constant-and-pointer-t1578/)

shabbir 9Oct2006 08:28

Constant and pointer
 
1 Attachment(s)

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: C

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: CPP

cout<<"Pointer to constant int"<<endl;
{
    int i = 123;
    const int * p = &i;
    cout <<"p = i = "<<*p<<endl;
    p = &i;
//  *p = 321; Pointer to constant 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: CPP

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: CPP

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"

aisha.ansari84 5Mar2008 18:55

Re: Constant and pointer
 
i m really confused with such sorts ,but your article gave some concept

peterdrew 21Jul2010 13:19

Re: Constant and pointer
 
It's very good; thanks!

fashionbop 22Jul2010 08:58

Re: Constant and pointer
 
i think so

Scripting 23Jul2010 15:53

Re: Constant and pointer
 
Really cool, shabbir.

THX for this

shabbir 23Jul2010 16:30

Re: Constant and pointer
 
The pleasure is mine.

qingqing 28Jul2010 08:26

Re: Constant and pointer
 
Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for.

gurumanoh 5Aug2010 18:50

Re: Constant and pointer
 
good work...

wdliming 4Oct2010 10:09

Re: Constant and pointer
 
good!! I like it!!

kien_vn 14Oct2010 16:13

Re: Constant and pointer
 
It's very clear to understand - "What is pointer"


All times are GMT +5.5. The time now is 01:41.