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


Groups > comp.lang.c > #21745

Re: Array sizes and const

From "BartC" <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Re: Array sizes and const
Date 2012-06-03 11:24 +0100
Organization A noiseless patient Spider
Message-ID <jqfe45$q7d$1@dont-email.me> (permalink)
References <jqdtou$koe$1@dont-email.me> <lnmx4lfk8a.fsf@nuthaus.mib.org> <jqe303$id9$1@dont-email.me>

Show all headers | View raw


"Les Cargill" <lcargill99@comcast.com> wrote in message 
news:jqe303$id9$1@dont-email.me...
> Keith Thompson wrote:

>> (Note that C++ has slightly different rules; in C++, a reference to
>> "size" *is* a constant expression.)
>>
>
> Right! We are in an infinity of twisty mazes :)

I've never got the hang of 'const' in C.

Using  it like this:

const int a = 1000;

means a gets the value 1000, then can't be changed. OK.

Now you try this:

int b=2000;
const int* p = &b;

means, I think, that you can't change b via the the pointer. But you *can* 
change p to point somewhere else. To protect p itself, it's necessary to 
write:

 int* const p = &b;

Now you can write: *p=3000; but you can't write p=&c. (I've just looked this 
up online which is how I know...)

To keep p readonly, and not allow it's target to be changed either, 
presumably you have to write:

const int * const p = &b;

Perfectly clear :-)

But, who care's about that; what I want to know is why, at some point during 
the last forty years, didn't someone just add a feature like:

 constant int d = 4000;

and it means exactly that: d is a synonym for the literal d. It has a 
precise type, and follows normal scoping rules. You also can't use it as an 
lvalue so it is guaranteed to have that value. It can also be used to 
initialise fixed size arrays, and does not to be written in capitals.

It's a five-minute job to add to the language (exaggerating a little, but it 
is pretty trivial to implement). Instead we have a series of unsatisfactory 
alternatives: #define, const (which doesn't work, or works but you 
unintentionally end up with a VLA), and enum{}, which you described as a 
'trick'...

-- 
Bartc 

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


Thread

Array sizes and const Les Cargill <lcargill99@comcast.com> - 2012-06-02 15:43 -0500
  Re: Array sizes and const Ian Collins <ian-news@hotmail.com> - 2012-06-03 08:56 +1200
  Re: Array sizes and const Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-06-02 16:59 -0400
    Re: Array sizes and const Les Cargill <lcargill99@comcast.com> - 2012-06-02 16:35 -0500
      Re: Array sizes and const jgk@panix.com (Joe keane) - 2012-06-08 20:05 +0000
      Re: Array sizes and const jgk@panix.com (Joe keane) - 2012-06-09 21:36 +0000
    Re: Array sizes and const Les Cargill <lcargill99@comcast.com> - 2012-06-02 16:43 -0500
  Re: Array sizes and const Keith Thompson <kst-u@mib.org> - 2012-06-02 14:59 -0700
    Re: Array sizes and const Les Cargill <lcargill99@comcast.com> - 2012-06-02 17:12 -0500
      Re: Array sizes and const Keith Thompson <kst-u@mib.org> - 2012-06-02 18:27 -0700
        Re: Array sizes and const Les Cargill <lcargill99@comcast.com> - 2012-06-02 22:27 -0500
      Re: Array sizes and const "BartC" <bc@freeuk.com> - 2012-06-03 11:24 +0100
        Re: Array sizes and const Keith Thompson <kst-u@mib.org> - 2012-06-03 13:51 -0700
          Re: Array sizes and const "BartC" <bc@freeuk.com> - 2012-06-04 12:20 +0100
            Re: Array sizes and const Jens Gustedt <jens.gustedt@loria.fr> - 2012-06-04 13:54 +0200
            Re: Array sizes and const Keith Thompson <kst-u@mib.org> - 2012-06-04 14:32 -0700
  Re: Array sizes and const Nobody <nobody@nowhere.com> - 2012-06-02 23:50 +0100
    Re: Array sizes and const Ian Collins <ian-news@hotmail.com> - 2012-06-03 10:53 +1200
      Re: Array sizes and const Nobody <nobody@nowhere.com> - 2012-06-03 14:35 +0100
    Re: Array sizes and const James Kuyper <jameskuyper@verizon.net> - 2012-06-02 21:11 -0400
      Re: Array sizes and const Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-06-03 07:39 -0700
        Re: Array sizes and const James Kuyper <jameskuyper@verizon.net> - 2012-06-03 16:03 -0400
        Re: Array sizes and const Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-03 21:49 +0100
          Re: Array sizes and const Keith Thompson <kst-u@mib.org> - 2012-06-03 14:49 -0700
          Re: Array sizes and const Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-06-03 15:02 -0700
            Re: Array sizes and const Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-04 01:21 +0100
        Re: Array sizes and const Ian Collins <ian-news@hotmail.com> - 2012-06-04 09:08 +1200
          Re: Array sizes and const Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-06-03 15:08 -0700
            Re: Array sizes and const Ian Collins <ian-news@hotmail.com> - 2012-06-04 10:37 +1200
              Re: Array sizes and const "BartC" <bc@freeuk.com> - 2012-06-03 23:47 +0100
                Re: Array sizes and const Ian Collins <ian-news@hotmail.com> - 2012-06-04 10:55 +1200
                Re: Array sizes and const Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-06-03 16:04 -0700
                Re: Array sizes and const Ian Collins <ian-news@hotmail.com> - 2012-06-04 11:14 +1200
                Re: Array sizes and const "BartC" <bc@freeuk.com> - 2012-06-04 00:23 +0100
                Re: Array sizes and const Ian Collins <ian-news@hotmail.com> - 2012-06-04 11:30 +1200
                Re: Array sizes and const "BartC" <bc@freeuk.com> - 2012-06-04 00:52 +0100
                Re: Array sizes and const Ian Collins <ian-news@hotmail.com> - 2012-06-04 12:04 +1200
                Re: Array sizes and const Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-04 01:39 +0100
                Re: Array sizes and const Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-06-04 02:35 -0700
                Re: Array sizes and const Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-04 01:33 +0100
                Re: Array sizes and const Ian Collins <ian-news@hotmail.com> - 2012-06-04 12:35 +1200
                Re: Array sizes and const pacman@kosh.dhis.org (Alan Curry) - 2012-06-04 21:28 +0000
        Re: Array sizes and const James Kuyper <jameskuyper@verizon.net> - 2012-06-04 12:08 -0400
        Re: Array sizes and const jgk@panix.com (Joe keane) - 2012-06-04 22:49 +0000
    Re: Array sizes and const David Brown <david.brown@hesbynett.no> - 2012-06-03 15:46 -0500
  Re: Array sizes and const Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-08 02:08 -0700
    Re: Array sizes and const Les Cargill <lcargill99@comcast.com> - 2012-06-08 06:45 -0500
      Re: Array sizes and const Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-08 10:49 -0700
        Re: Array sizes and const Les Cargill <lcargill99@comcast.com> - 2012-06-08 18:05 -0500
    Re: Array sizes and const Keith Thompson <kst-u@mib.org> - 2012-06-08 13:31 -0700
      Re: Array sizes and const Tim Rentsch <txr@alumni.caltech.edu> - 2012-06-08 20:30 -0700

csiph-web