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


Groups > comp.lang.c > #43360

Re: const or constant?

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.c
Subject Re: const or constant?
Date 2014-04-22 21:26 +0000
Organization Freie Universitaet Berlin
Message-ID <bro54nFojnqU1@mid.uni-berlin.de> (permalink)
References <lj6729$a10$1@speranza.aioe.org>

Show all headers | View raw


jacob navia <jacob@spamsink.net> wrote:
> After reading some of the contrbutions to the "constant strings" thread, 
> I think that what is needed is a clarification of the underlying issues.

> There are TWO "uses" of const:

> 1) Declaring that a function will not modify its arguments
> 2) Declaring that an object resides in ROM (read only memory)

There's no requirement that a constant of type 2) is in ROM.
What about a function like

void foo( int v ) {
    const int d = v;
	...
}

Why should be 'd' in ROM? It only makes sure that the compiler
will get upset when you try to change its value sometime later
in the function.

And you also seem to be forgetting that 1) and 2) can be
combined as in

  const char * const s

i.e. a pointer that can't be changed to a region of memory
that also can't be modified.

> The first one is tied to a *scope*. 

I don't see that this has anything to do with scope. The pit-
fall, I guess, is that e.g.

  const char * s

doesn't mean that the value of 's' can't be changed (which
one might assume) but what it points to. I.e., for a func-
tion declared as 

   void bar( const char * s, const int d );

it is not really obvious for a beginner that the value of
'd' can't be changed within the function, but that of 's'
can (but not what 's' points to). It makes more sense when
one reads it out aloud as "s is a pointer to const char" -
you need to read it backwards. Reading it backword also

  const char * const s

makes sense: it's a "constant pointer to const chars".

> no modifications are done to some object. I would propose that we name 
> this property "constant" and we would declare:

> char *strchr(constant char *buffer, int searchedChar);

Not only is this unfeasible (it would break millions of
programs) but it doesn't really seem to make anything
much clearer. For that you would need a different way
of declaring/defining pointer. If you could write

  * char s;              <=> char * s;

for "pointer to char", and

  * const char s;        <=> const char * s;

for "pointer to constant char", and

  const * char s;        <=> char * const char;

for "constant pointer to char", and, finally

  const * const char s;  <=> const char * const s;

for "constant pointer to const char", then you'd avoid the
necessity to read it backwards for pointers which, in my
opinion, is what can be confusing.

Another idea would be to use the "address of" operator, '&'
in declarations/definitions instead of '*', which isn't really
very logical, e.g.

   &char s;              <=>   char * s';

would mean "s is the address of a char", i.e. a pointer to a
char, a would be nicely similar to a simple

  char s;

Then you'd have

  &const char s;         <=>   const char * s;

and

  const &char s;         <=>   char * const s;

and, finally

  const &const char s;   <=>  const char * const s;

That way it would make a bit more sense since it could be
read left to right and the '&' seems to be more appropriate
in this context than the '*'.

But while you're at it: perhaps you've also an idea how to
make function pointers more accessible?;-)

                         Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

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


Thread

const or constant? jacob navia <jacob@spamsink.net> - 2014-04-22 18:58 +0200
  Re: const or constant? "Bill Cunningham" <nospam@nspam.invalid> - 2014-04-22 14:13 -0400
    Re: const or constant? Barry Schwarz <schwarzb@dqel.com> - 2014-04-22 15:59 -0700
  Re: const or constant? jt@toerring.de (Jens Thoms Toerring) - 2014-04-22 21:26 +0000
    Re: const or constant? James Kuyper <jameskuyper@verizon.net> - 2014-04-22 17:56 -0400
      Re: const or constant? Keith Thompson <kst-u@mib.org> - 2014-04-22 15:32 -0700
        Re: const or constant? James Kuyper <jameskuyper@verizon.net> - 2014-04-23 08:57 -0400
          Re: const or constant? David Brown <david.brown@hesbynett.no> - 2014-04-23 15:27 +0200
            Re: const or constant? Keith Thompson <kst-u@mib.org> - 2014-04-23 08:34 -0700
              Re: const or constant? David Brown <david.brown@hesbynett.no> - 2014-04-24 08:50 +0200
                Re: const or constant? James Kuyper <jameskuyper@verizon.net> - 2014-04-24 07:58 -0400
                Re: const or constant? David Brown <david.brown@hesbynett.no> - 2014-04-24 14:19 +0200
                Re: const or constant? glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-04-24 21:40 +0000
                Re: const or constant? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-04-25 01:05 -0700
          Re: const or constant? "BartC" <bc@freeuk.com> - 2014-04-23 21:38 +0100
      Re: const or constant? jacob navia <jacob@spamsink.net> - 2014-04-23 00:51 +0200
    Re: const or constant? jacob navia <jacob@spamsink.net> - 2014-04-23 00:47 +0200
  Re: const or constant? Walter Banks <walter@bytecraft.com> - 2014-04-22 18:00 -0400
  Re: const or constant? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-04-22 23:12 +0100
    Re: const or constant? jacob navia <jacob@spamsink.net> - 2014-04-23 01:00 +0200
      Re: const or constant? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-04-23 14:48 +0100
        Re: const or constant? Keith Thompson <kst-u@mib.org> - 2014-04-23 08:40 -0700
          Re: const or constant? jacob navia <jacob@spamsink.net> - 2014-04-23 21:49 +0200
            Re: const or constant? Kaz Kylheku <kaz@kylheku.com> - 2014-04-23 20:11 +0000
            [OT] Killfiles (was Re: const or constant?) Keith Thompson <kst-u@mib.org> - 2014-04-23 14:49 -0700
              Re: [OT] Killfiles (was Re: const or constant?) Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-04-23 22:22 -0700
    Re: const or constant? James Kuyper <jameskuyper@verizon.net> - 2014-04-23 09:09 -0400
  Re: const or constant? Ian Collins <ian-news@hotmail.com> - 2014-04-24 09:35 +1200
    Re: const or constant? Ian Collins <ian-news@hotmail.com> - 2014-04-24 10:06 +1200
      Re: const or constant? Ian Collins <ian-news@hotmail.com> - 2014-04-24 10:52 +1200

csiph-web