Do something to improve things. Anything. Start small or go big. Just do something. Today. Now. The world will be better for it. ----Just because no one else believes in your dreams doesn't mean you shouldn't believe in your dreams. ----The world is the great gymnasium where we come to make ourselves strong. "Swami Vivekananda"

Government Jobs In India Headline Animator

04 August 2010

Getting into the constants and pointer

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



Code: CPP

cout<<"Constant pointer"<

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

No comments:

Subscribe via email

Enter your email address:

Delivered by FeedBurner

website-hit-counters.com
Provided by website-hit-counters.com site.