Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #163252

Re: on the order of the const-keyword

From Meredith Montgomery <mmontgomery@levado.to>
Newsgroups comp.lang.c
Subject Re: on the order of the const-keyword
Date 2021-10-29 17:04 -0300
Organization Aioe.org NNTP Server
Message-ID <86ilxfha1n.fsf@levado.to> (permalink)
References <86mtmtm9m1.fsf@levado.to> <slg9ru$nmg$1@dont-email.me> <86lf2bj1pj.fsf@levado.to> <slh7rf$qai$1@dont-email.me>

Show all headers | View raw


David Brown <david.brown@hesbynett.no> writes:

> On 29/10/2021 17:22, Meredith Montgomery wrote:
>> David Brown <david.brown@hesbynett.no> writes:
>> 
>>> On 28/10/2021 17:50, Meredith Montgomery wrote:
>>>> These seem legal declarations in C:
>>>>
>>>>   const char msg[9] = "warming:";
>>>>   char const msg[9] = "warming:";
>>>>
>>>> What's the difference between them?
>>>>
>>>
>>> Nothing.
>>>
>>> Both mean that the array is made of "const char" elements - after you
>>> have initialised the array, you can't change them.
>>>
>>>
>>> A lot of people prefer the "const on the left" arrangement :
>>>
>>> 	const int x = 123;
>>>
>>> It is very common to use this ordering - many programmers feel it is
>>> more natural.
>>>
>>>
>>> Some people prefer the "const on the right" arrangement (known as "east
>>> const") :
>>>
>>> 	int const x = 123;
>>>
>>> The argument there is that it is more consistent, especially in C++
>>> (which has a few other possible uses of the word "const").
>>>
>>>
>>>
>>> It is fair to say that the "east const" is more consistent when you have
>>> more complicated declarations involving pointers with "const" applied to
>>> different parts.  That does not mean that it is the clearest way to
>>> write simple declarations, which are far more common.  So opinions
>>> differ here.  You have to learn to understand the placement options as
>>> you will see both when you read other people's code, and you have to
>>> decide for yourself which you choose in your own code (unless you are
>>> using a set of style guidelines that forces the decision on you).
>>>
>>>
>>> The general rule is "const applies to the thing just to the left of it,
>>> unless there is nothing to the left of it and it then applies to the
>>> thing to the right of it".  This fits well with the usual best tactic
>>> for parsing most types - start from the right and work left.
>> 
>> Pretty useful guidance.  Thank you.  I will try that from now on.
>> 
>> You spoke of consistency above, but your paragraphs sometimes skip one
>> line, sometimes two lines, sometimes three lines.  What's up with that?
>> 
>>                                   :-)
>> 
>
> It's to give you time to think and absorb the information underway!
> Also, it gives a visual separation that is greater than I used for the
> code snippets - so the spacing is intentional.  (I know your question
> wasn't very serious.)

It wasn't, yes, but that's because I thought there wasn't a special
reason.  Now that I know there is, I'm impressed and a bit surprised, so
now I'm glad I asked.

Here's what I probably would have done.

--8<---------------cut here---------------start------------->8---
(*) Introduction

Lorem ipsum dolor... 

(*) Now to your question specifically

Lorem...

(*) Taking stock or something

Lorem ipsum...
--8<---------------cut here---------------end--------------->8---

If you think about it, your style up there is a a bit too implicit.  If
I'd like to give the reader some time to ponder, I would say that
explicitly --- dear reader, please take some time to ponder.  

Having said that, I never cast the return of malloc. :-)

Back to comp.lang.c | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

on the order of the const-keyword Meredith Montgomery <mmontgomery@levado.to> - 2021-10-28 12:50 -0300
  Re: on the order of the const-keyword Bart <bc@freeuk.com> - 2021-10-28 17:25 +0100
    Re: on the order of the const-keyword "james...@alumni.caltech.edu" <jameskuyper@alumni.caltech.edu> - 2021-10-28 10:10 -0700
      Re: on the order of the const-keyword Guillaume <message@bottle.org> - 2021-10-28 20:40 +0200
      Re: on the order of the const-keyword Meredith Montgomery <mmontgomery@levado.to> - 2021-10-29 00:07 -0300
        Re: on the order of the const-keyword James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-29 00:48 -0400
    Re: on the order of the const-keyword Thiago Adams <thiago.adams@gmail.com> - 2021-10-28 12:56 -0700
      Re: on the order of the const-keyword Thiago Adams <thiago.adams@gmail.com> - 2021-10-28 13:18 -0700
        Re: on the order of the const-keyword Bart <bc@freeuk.com> - 2021-10-28 21:35 +0100
          Re: on the order of the const-keyword Thiago Adams <thiago.adams@gmail.com> - 2021-10-28 13:57 -0700
            Re: on the order of the const-keyword Thiago Adams <thiago.adams@gmail.com> - 2021-10-28 14:07 -0700
              Re: on the order of the const-keyword Bart <bc@freeuk.com> - 2021-10-28 22:54 +0100
              Re: on the order of the const-keyword Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-28 15:37 -0700
                Re: on the order of the const-keyword Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-10-29 00:02 +0100
        Re: on the order of the const-keyword Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-10-28 22:10 +0100
        Re: on the order of the const-keyword Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-28 15:13 -0700
        Re: on the order of the const-keyword Meredith Montgomery <mmontgomery@levado.to> - 2021-10-29 00:40 -0300
    Re: on the order of the const-keyword Meredith Montgomery <mmontgomery@levado.to> - 2021-10-28 23:48 -0300
  Re: on the order of the const-keyword James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-28 13:04 -0400
    Re: on the order of the const-keyword Meredith Montgomery <mmontgomery@levado.to> - 2021-10-29 00:34 -0300
  Re: on the order of the const-keyword David Brown <david.brown@hesbynett.no> - 2021-10-29 10:03 +0200
    Re: on the order of the const-keyword Bart <bc@freeuk.com> - 2021-10-29 11:30 +0100
      Re: on the order of the const-keyword David Brown <david.brown@hesbynett.no> - 2021-10-29 16:31 +0200
        Re: on the order of the const-keyword Bart <bc@freeuk.com> - 2021-10-29 16:45 +0100
          Re: on the order of the const-keyword David Brown <david.brown@hesbynett.no> - 2021-10-29 18:30 +0200
            Re: on the order of the const-keyword Manfred <noname@add.invalid> - 2021-10-30 00:32 +0200
              Re: on the order of the const-keyword Meredith Montgomery <mmontgomery@levado.to> - 2021-10-29 20:03 -0300
              Re: on the order of the const-keyword Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-10-30 01:10 +0100
            Re: on the order of the const-keyword Bart <bc@freeuk.com> - 2021-10-29 23:51 +0100
    Re: on the order of the const-keyword Meredith Montgomery <mmontgomery@levado.to> - 2021-10-29 12:22 -0300
      Re: on the order of the const-keyword David Brown <david.brown@hesbynett.no> - 2021-10-29 18:34 +0200
        Re: on the order of the const-keyword Meredith Montgomery <mmontgomery@levado.to> - 2021-10-29 17:04 -0300

csiph-web