This is scary 
Author Message
 This is scary

I would like to point out the differences between

int const a, b;
and
int * const pa, pb;

if I am correct, b is const while pb isn't!

Please correct me if I'm wrong.
--
"Pedants make the best programmers" - Richard Heathfield
"That is probably a misquote, but
 I like it nonetheless" - Finny Merrill

Indent-o-meter
        01234567
        ^



Fri, 16 Jul 2004 12:41:24 GMT  
 This is scary

Quote:
> I would like to point out the differences between

> int const a, b;
> and
> int * const pa, pb;

> if I am correct, b is const while pb isn't!

You're, IMHO, but also note that pb isn't a pointer either.
I very much liked Peter van der Lindens descriptions of how to
read complicated declarations and definitions in his book
"Expert C Programming". The precedence rules Peter mentions
also state:

  ...
 C If a const and/or volatile keyword is next to a type
   specifier (e.g. int, long, etc.) it applies to the type
   specifier. Otherwise the const and/or volatile keyword
   applies to the pointer asterisk on its immediate left.

That means:

int const *p;
const int *q;
are pointers to read-only ints, whereas

int * const r;
is a read-only pointer to an int.

it gets sort of complicated when we have more than one
definiton/declaration on one line:

int const *p, i, * const q;

here p ist a pointer to a read-only int, i is such a read-only
int and q is read-only pointer to a read-only int.

HTH, HAND
--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Fri, 16 Jul 2004 20:55:11 GMT  
 This is scary

Quote:

> I would like to point out the differences between

> int const a, b;
> and
> int * const pa, pb;

> if I am correct, b is const while pb isn't!

> Please correct me if I'm wrong.

What did your compiler say?
--
Minti.


Fri, 16 Jul 2004 21:34:38 GMT  
 This is scary

Quote:

>> I would like to point out the differences between

>> int const a, b;
>> and
>> int * const pa, pb;

>> if I am correct, b is const while pb isn't!

>> Please correct me if I'm wrong.

> What did your compiler say?

The compiler may say whatever it wants about it. It probably
will bail out when you later try to use pb as a pointer or try
tp write to pa, a or b. But pb and *pa both may be used
as integers (if pa would have been initialized with a propper
address of an int object, like a or b or pb).
--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond


Fri, 16 Jul 2004 21:27:53 GMT  
 This is scary

Quote:

>I would like to point out the differences between

>int const a, b;
>and
>int * const pa, pb;

>if I am correct, b is const while pb isn't!

>Please correct me if I'm wrong.

Perhaps more importantly, pa is a pointer (const-qualified poitner to
int), whereas pb isn't.

Object   Type
--------------------
a        const int
b        const int
pa       int * const
pb       int



Wed, 21 Jul 2004 14:15:28 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Scary UB

2. Top 10 scariest posters to comp.lang.c

3. Want to hear something scary?

4. Now *this* is scary

5. I am new to programming and am lost

6. how good am I? Am I Good Enough????

7. Determine where I am running

8. System.Threading.Timer , am I doing this correctly ?

9. Am i connected?

10. Request - Opinions on Book I am considering:

11. I am puzzled: __nogc new = LNK2001

12. Am I in the wrong NG???

 

 
Powered by phpBB® Forum Software