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


Groups > comp.lang.c > #2944

Re: Need some info on const char **

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.lang.c
Subject Re: Need some info on const char **
Date 2011-04-27 00:10 +0000
Organization Freie Universitaet Berlin
Message-ID <91p572Fu90U1@mid.uni-berlin.de> (permalink)
References <h3ler6dnl5jk821uhfvjvfcldgc8eqptel@4ax.com>

Show all headers | View raw


DSF <notavalid@address.here> wrote:
>   I have written several functions that take a char ** pointer.  Since
> these functions do not alter the pointer array or the character
> arrays, I feel it's good to label the pointer as const.

>   I have two concerns:

>   1. I'm a little rusty on the meaning of const in this case.  Does it
> indicate that the pointer array will not be altered, the character
> arrays will not be altered, or both?  What are the usable
> permutations?

It depends on where you put the 'const'. E.g. with

void foo( const char * const * bar )

you tell that the function is neither going to change what
the 'bar' points to nor what the result of dereferencing
'bar' points to.

So the question is what you want to be considered as 'const':

a) what the pointer points to *and* what the result of
   dereferencing it points to
b) only what the pointer points to
c) only what is pointed to when dereferencing the pointer

So you have

a) const char * const * bar

  -> declare 'bar' as pointer to const pointer to const char

b) char * const * bar

  -> declare 'bar' as pointer to const pointer to char

c) const char ** bar

  -> declare 'bar' as pointer to pointer to const char

Take your pick;-)

>   2. Any call to say,

> int foo(const char **ca);

This declares a function that takes a pointer to a pointer
to const char - you're allowed to change what 'ca' points
to but not what's pointed to by what 'ca' points to (per-
haps an immutable array). I.e. if foo() is callled like
this

const char * a = "Hello";
foo( &a );

then you can do in foo():

void foo( const char **ca )
{
    *ca = "world";
}

and then, afterwards in the caller, 'a' will point to the
(immutable) string "world". But you can't do

void foo( const char ** ca )
{
    **ca = 'X';
}

to have what 'a' points to in the caller to be changed to
"Xello".

If in doubt what the "gibberish" means (i.e. where to put
the 'const') the 'cdecl' program (which can also to be used
via <http://cdecl.org/>) can be quite helpful.

> int r;
> char *c;
> char **c_array;
> ...
> r = foo(c_array);

>   requires a cast

> r = foo((const **)c_array);

I don't think the compiler will really like this (it could
be upset about the missing type and maybe mumble something
about using 'int' as a default)...

>   Is it better to use const and cast each call to foo, or omit the
> const?  On one hand, identifying ca as const seems the right thing to
> do, but on the other hand, requiring the cast allows for bugs.

Specify exactly what your function expects. Then there's no
room left for mistakes, neither by you nor by the compiler.
Then avoid casts, they just tell the compiler "that func-
tion wants something different from what I'm passing it, but
I know better, for some reasons you're too stupid to under-
stand, what it will get is what it expects. Just trust me
and don't bother me with what you may consider to be helpful
but actually are just annoying warnings."

If a function expects something it's to treat as constant but
gets passed something non-constant than there's no problem at
all. But when a function expects something it can change but
the compiler is asked to pass it something that is declared
as constant than there's typically a problem. Sure, you can
dismiss the compiler's concerns with a cast, but when you do
that you'd better have a real good reason.

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

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


Thread

Need some info on const char ** DSF <notavalid@address.here> - 2011-04-26 18:49 -0500
  Re: Need some info on const char ** jt@toerring.de (Jens Thoms Toerring) - 2011-04-27 00:10 +0000
    Re: Need some info on const char ** DSF <notavalid@address.here> - 2011-04-27 00:38 -0500
      Re: Need some info on const char ** jt@toerring.de (Jens Thoms Toerring) - 2011-04-27 07:58 +0000
        Re: Need some info on const char ** Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-04-27 15:41 +0100
          Re: Need some info on const char ** lawrence.jones@siemens.com - 2011-04-27 14:42 -0400
        Re: Need some info on const char ** DSF <notavalid@address.here> - 2011-04-27 19:19 -0500
          Re: Need some info on const char ** Ike Naar <ike@iceland.freeshell.org> - 2011-04-28 06:28 +0000
            Re: Need some info on const char ** DSF <notavalid@address.here> - 2011-04-29 00:47 -0500
              Re: Need some info on const char ** Ike Naar <ike@sverige.freeshell.org> - 2011-04-29 09:13 +0000
                Re: Need some info on const char ** Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-04-29 16:08 +0100
                Re: Need some info on const char ** DSF <notavalid@address.here> - 2011-05-03 01:47 -0500
                Re: Need some info on const char ** Ike Naar <ike@otaku.freeshell.org> - 2011-05-03 07:45 +0000
                Re: Need some info on const char ** Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-03 11:55 +0100
                Re: Need some info on const char ** DSF <notavalid@address.here> - 2011-05-04 20:20 -0500
                Re: Need some info on const char ** Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-05-05 02:57 +0100
                Re: Need some info on const char ** DSF <notavalid@address.here> - 2011-05-07 22:03 -0500
                Re: Need some info on const char ** DSF <notavalid@address.here> - 2011-05-04 19:53 -0500
                Re: Need some info on const char ** falk@rahul.net (Edward A. Falk) - 2011-05-05 02:01 +0000
                Re: Need some info on const char ** Ike Naar <ike@sverige.freeshell.org> - 2011-05-05 05:51 +0000
                Re: Need some info on const char ** DSF <notavalid@address.here> - 2011-05-07 22:04 -0500
      Re: Need some info on const char ** Paul N <gw7rib@aol.com> - 2011-04-27 14:03 -0700

csiph-web