Deutsch   English   Français   Italiano  
<vibr1l$vvjf$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: question about linker
Date: Fri, 29 Nov 2024 08:38:29 +0100
Organization: A noiseless patient Spider
Lines: 123
Message-ID: <vibr1l$vvjf$1@dont-email.me>
References: <vi54e9$3ie0o$1@dont-email.me> <vi6sb1$148h7$1@paganini.bofh.team>
 <vi6uaj$3ve13$2@dont-email.me> <87plmfu2ub.fsf@nosuchdomain.example.com>
 <vi9jk4$gse4$1@dont-email.me> <vi9kng$gn4c$1@dont-email.me>
 <87frnbt9jn.fsf@nosuchdomain.example.com> <viaqh0$nm7q$1@dont-email.me>
 <877c8nt255.fsf@nosuchdomain.example.com> <viasv4$nm7q$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 29 Nov 2024 08:38:35 +0100 (CET)
Injection-Info: dont-email.me; posting-host="eea823d2fe9082c26554765f8f5076a5";
	logging-data="1048175"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19lF2lqM0MILdUsc2u6sHke3B9sjykVSqE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:HntUfJxozs0US4vtHAI3EqVIeUo=
In-Reply-To: <viasv4$nm7q$2@dont-email.me>
Content-Language: en-GB
Bytes: 5949

On 29/11/2024 00:05, Bart wrote:
> On 28/11/2024 22:38, Keith Thompson wrote:
>> Bart <bc@freeuk.com> writes:
>>> 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 both
>>>>> confusing and awkward to implement.
>>>>>
>>>>> That's because both really apply to /types/, not directly to 
>>>>> variables.
>>>> Sure.  For example, given
>>>>       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.
> 
> Well, can you see 'const' in my example? You can't tell x is readonly by 
> only looking at this.
> 
> 
>> 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;

That one is really simple - clearly "x" is declared "const", and so you 
can't assign to it later.

>      T    const y;

That one is equally simple - clearly "y" is declared "const", and so you 
can't assign to it later.

That shows exactly why it can be a good idea to use "typedef", even for 
relatively simple things such as adding a pointer or qualifiers to the 
type.  I would not normally make a typedef just for a pointer, or just 
to add a "const" qualifier, but if the final type is complicated, it can 
make things clearer.  Really, it's just like breaking a complicated 
expression into parts with extra local variables to name them.

>      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;
>      y=0;
>      z=0;
> 
> because I thought would behave differently, with 'const' being the 
> opposite side of '*' to the base-type.
> 

The "const" in each case clearly applies to the type of the declared 
variable.

> I forgot that here it would be the right-most 'const' that controls 
> storage attributes of 'z'.
> 
> You will of course say that I'm the only person in the world who could 
> make that mistake.
> 

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.

There are two types of C programmers - those who like to declare 
variables "const" on a regular basis, and those who rarely if ever do 
(reserving "const" for things like "const int *" pointers).  Those that 
declare const variables, initialise them at the time, and would not be 
trying to assign them later.  Those who don't have much use of const 
variables, would not be writing such code in the first place.

Basically, I'd only expect to see examples like that in the questions 
section of a beginner's book on C.  And I'd expect anyone who had read 
the preceding chapter to be able to answer it.


C's syntax for types certainly can be confusing and awkward, especially 
in complex situations.  Mix pointers, qualifiers, function types, and 
array syntax together and you can easily make a mess that will take a 
lot of effort to unpick.

So the answer is /very/ simple - don't do that.

Make an effort to write your own types clearly, in a way that makes them 
obvious to the reader.  "typedef" is your friend.  If you have a very 
complex type (which are rare in practice, but do occur), build it in 
parts with typedefs, and give it a typedef itself.  Then it is vastly 
easier to use multiple times.

You might occasionally have to understand someone else's messy type, but 
you can at least make life easier for yourself.  I certainly do that.