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

866cb393e25d89f9b17096b7849c00ba
0
aryasir 101 Aug 31, 2005 at 00:59

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.

4 Replies

Please log in or register to post a reply.

C24eb7e6aaefba78b94c831ddc7b4d0b
0
donBerto 101 Aug 31, 2005 at 03:35

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:

22b3033832c5c699c856814b0cf80cb1
0
bladder 101 Aug 31, 2005 at 05:21

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.

6481ce6f773f7a3e6f01f143a6d0fa3d
0
Arjen 101 Aug 31, 2005 at 06:39

@bladder

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.

[snapback]20554[/snapback]

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.

22b3033832c5c699c856814b0cf80cb1
0
bladder 101 Aug 31, 2005 at 07:07

woops, right my bad :wallbash: