Jump to content


difference between "const int i" and "int const i"


4 replies to this topic

#1 aryasir

    New Member

  • Members
  • Pip
  • 2 posts

Posted 31 August 2005 - 12:59 AM

Is there a difference between the following two lines:

const int i = 0;
int const i = 0;

apparently, I was told they are different, so I wanted to know the difference.

#2 donBerto

    Senior Member

  • Members
  • PipPipPipPip
  • 369 posts

Posted 31 August 2005 - 03:35 AM

hehe one of my professors taught us this. too bad for me, I wasn't paying attention. thank god for google.

check out the following week. your question can be answered by, "More Geeky Stuff" at the bottom.
http://www.phim.unibe.ch/comp_doc/c_manual...NTAX/const.html

regards,
:yes:
Imagine.

#3 bladder

    DevMaster Staff

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 31 August 2005 - 05:21 AM

They're exactly the same, the difference with the positioning of "const" comes into play when you use pointers.

const int* is a constant pointer to integer data, that means you cant change the pointer itself to point to something else.

int* const is a pointer to constant integer data, which means you can change the pointer, but you can't change the data it points to.

#4 Arjen

    New Member

  • Members
  • Pip
  • 2 posts

Posted 31 August 2005 - 06:39 AM

bladder said:

const int* is a constant pointer to integer data, that means you cant change the pointer itself to point to something else.

int* const is a pointer to constant integer data, which means you can change the pointer, but you can't change the data it points to.

View Post

That would be the other way around.

const int* -> a (variable) pointer to constant integer data
int* const -> a constant pointer to integer data

The trick is reading from right to left.

#5 bladder

    DevMaster Staff

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 31 August 2005 - 07:07 AM

woops, right my bad :wallbash:





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users