Sujet : Re: question about linker
De : ike (at) *nospam* sdf.org (Ike Naar)
Groupes : comp.lang.cDate : 29. Nov 2024, 13:06:14
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <slrnvkjbhl.78r.ike@iceland.freeshell.org>
References : 1 2 3 4 5 6 7 8 9 10 11 12
User-Agent : slrn/1.0.3 (Patched for libcanlock3) (NetBSD)
On 2024-11-29, Bart <
bc@freeuk.com> wrote:
These are similar examples:
>
int * const z1;
int const * z2;
>
z1=0; // invalid
z2=0; // valid
>
[snip]
>
Both 'const' in my new examples are the the right-most one! Yet one
makes the immediate storage const and one doesn't. I guess then that
it's the right-most possible 'const' if it were to be used. In my
example, that would follow the '*'.
The order in which '*' and 'const' appear matters.
With
int * const z1;
the const applies to z1 because it appears immediately before 'z1'.
z1 is a const pointer to int.
(hint: read the declaration out loud from right to left).
*z1 = 0; /* valid */
z1 = 0; /* invalid, z1 is readonly */
With
int const * z2;
the const applies to *z2 because it appears immediately before '* z2'.
*z2 is a const int, z2 is a pointer to const int.
(again, read the declaration out loud from right to left).
*z2 = 0; /* invalid, *z2 is readonly */
z2 = 0; /* valid */
With
int const * const z3;
the leftmost const applies to *z3 and the rightmost const applies to z3.
z3 is a const pointer to const int.
*z3 = 0; /* invalid, *z3 is readonly */
z3 = 0; /* invalid, z3 is readonly */