Question about declaring const pointer to const value

Question about declaring const pointer to const value

Post by linq93 » Wed, 19 Sep 2007 13:24:57


Hi,
I have the following code:

int main(){ /* line 1 */
const int i = 10; /* line 2 */
const int j = 20; /* line 3 */
/* line 4 */
int const * const p = &i; /* line 5 */
const int * const p2 = &j; /* line 6 */
/* line 7 */
(*p2)++; /* line 8 */
(*p)++; /* line 9 */

return 0;
}

Compiler does not complain line 6 and errors out on those
incremental statements of line 8 and 9.

So the declaration on line 5 and line 6 are the same? I mean "int
const * const" and "const int * const" are same?

It is a little hard to grasp how to use "const" in declaration.

Any tip?
 
 
 

Question about declaring const pointer to const value

Post by CBFalcone » Wed, 19 Sep 2007 14:58:54


The thing that counts is which side of the * the 'const' is.
"const int *p" means p is a non-constant pointer which points at
read-only int(s). "int * const p = &x;" means p is a read-only
pointer, set to point at the variable x, and which can only be set
at this declaration point.

Try to use fixed width fonts, so as to avoid the silly widths.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
< http://www.yqcomputer.com/ >



--
Posted via a free Usenet account from http://www.yqcomputer.com/

 
 
 

Question about declaring const pointer to const value

Post by rlb » Wed, 19 Sep 2007 15:27:40


Yes. Read the friggin' FAQ already.

< http://www.yqcomputer.com/ ;

Richard