Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Question about declaring const pointer to const value

Reply
Thread Tools

Question about declaring const pointer to const value

 
 
linq936
Guest
Posts: n/a
 
      09-18-2007
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?

 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      09-18-2007
linq936 wrote:
>
> 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.


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://cbfalconer.home.att.net>



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

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      09-18-2007
linq936 <> wrote:

> 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?


Yes. Read the friggin' FAQ already.

<http://c-faq.com/ansi/constptrconst.html>

Richard
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const pointer versus const content pointed to by pointer Disc Magnet C Programming 1 05-06-2010 10:27 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Smart Pointer release() const : it can set the pointer to null with the keyword "const"? coala C++ 1 09-06-2006 03:00 PM
Smart Pointer release() const : it can set the pointer to null with the keyword "const"? coala C++ 3 09-06-2006 02:58 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57