Liste des Groupes | Revenir à cl c |
On 28/11/2024 22:38, Keith Thompson wrote:That one is really simple - clearly "x" is declared "const", and so you can't assign to it later.Bart <bc@freeuk.com> writes:Well, can you see 'const' in my example? You can't tell x is readonly by only looking at this.On 28/11/2024 19:58, Keith Thompson wrote:>Bart <bc@freeuk.com> writes:>
[...]I think 'const' is confusing for similar reasons that VLAs can be bothSure. For example, given
confusing and awkward to implement.
>
That's because both really apply to /types/, not directly to variables.
const int n = 42;
n is of type `const int`, and &n is of type `consts int*`. Of course
that implies that n itself is const.
But that is a separate thing. Suppose T was an alias for 'const int'. Then:
>
T x; // defines a readonly variable (which probably needs
// initialising)
T* y; // defines a variable pointer
>
'const' is out of the picture.
You say T is an alias (what, a macro?) for 'const int', you show code
using T, and then you say "'const' is out of the picture". If you have
a point, it escapes me.
Yes, and you seem determines to make it easier to get mixed up.C doesn't require any help from me for confusing features. The OP said it was confusing and I tried to point out why it might be.
Obviously you as C expert will never be confused. But there are lots of less expert users of the language.
I've just several minutes trying to figure why all these assignments are invalid:
typedef int* T;
int const x;
T const y;That one is equally simple - clearly "y" is declared "const", and so you can't assign to it later.
int* const z;This one requires a little more thought, but it should be well within the capacity of any C programmer to see that "z" is declared "const".
x=0;The "const" in each case clearly applies to the type of the declared variable.
y=0;
z=0;
because I thought would behave differently, with 'const' being the opposite side of '*' to the base-type.
I forgot that here it would be the right-most 'const' that controls storage attributes of 'z'.I certainly don't say you are the only person in the world who /could/ make that mistake. But if we narrow it down to the C programmers who /would/ make such mistakes, you are in a much smaller group.
You will of course say that I'm the only person in the world who could make that mistake.
Les messages affichés proviennent d'usenet.