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


Groups > comp.lang.c > #21769

Re: Array sizes and const

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Array sizes and const
Date 2012-06-03 14:49 -0700
Organization None to speak of
Message-ID <lntxysdq1e.fsf@nuthaus.mib.org> (permalink)
References <jqdtou$koe$1@dont-email.me> <pan.2012.06.02.22.50.02.517000@nowhere.com> <jqedjj$9gi$1@dont-email.me> <39346056-1528-4667-85d2-a860b6ba018a@googlegroups.com> <0.c7cbb34dce2283fc4e7f.20120603214919BST.87sjecce9c.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
>> בתאריך יום ראשון, 3 ביוני 2012 02:11:15 UTC+1, מאת James Kuyper:
>>> On 06/02/2012 06:50 PM, Nobody wrote:
>>> ...
>>> > The main purpose of "const" is to tell the compiler to assume that the
>>> > value won't change.
>>> 
>>> I don't think that's the main purpose. In my opinion, the most important
>>> thing about 'const', and the reason why I use it where appropriate, is
>>> that it turns many dangerous constructs into constraint violations,
>>> making a diagnostic mandatory.
>>> 
>> I never use it. Part of the reason is that whilst int member(const FOO
>> *obj) is clear enough in intention, in fact the constness of obj does
>> not extend to its members.
>
> Can you say what you mean by this?  It contradicts what I thought was
> the case (assuming you meant the members of *obj).
>
> <snip>

Agreed.

typedef struct foo {
    int x;
} FOO;

int member(const FOO *obj) {
    obj->x = 42;
    return 43;
}

int main(void) {
    FOO foo_obj;
    int m = member(&foo_obj);
    return 0;
}

c.c: In function ‘member’:
c.c:6:5: error: assignment of member ‘x’ in read-only object

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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