Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Need some info on const char ** |
| Date | 2011-04-27 07:58 +0000 |
| Organization | Freie Universitaet Berlin |
| Message-ID | <91q0l9Fne0U1@mid.uni-berlin.de> (permalink) |
| References | <h3ler6dnl5jk821uhfvjvfcldgc8eqptel@4ax.com> <91p572Fu90U1@mid.uni-berlin.de> <7h5fr6p7g8pgdaeohia1dlumnl7tha9nlk@4ax.com> |
DSF <notavalid@address.here> wrote:
> On 27 Apr 2011 00:10:10 GMT, jt@toerring.de (Jens Thoms Toerring)
>
> >> 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)...
> What missing type? The above line is what's necessary for the
> compiler to not complain.
The '(const **)' bit is missing the type to cast to, it
just says 'cast to pointer to const pointer of something
of uspecified type' and I guess you just missed the 'char'
bit in there. At least my compiler doesn't find that just
right and tells me it's using 'int' as the default type.
> >Specify exactly what your function expects. Then there's no
> >room left for mistakes, neither by you nor by the compiler.
> I've thought of it as not what my function expects, but rather a
> promise as to what my function won't do. The declaration
> int foo(const char * const *ca);
> promises that foo will not change anything through ca. Not the array
> pointers, nor the strings themselves.
Both are a bit of the truth. The 'const' makes a promise
that the function will refrain from changing something
but it also may out some obligation on the types the
caller can pass it, which is just what's happening in
your case, see below.
> >If a function expects something it's to treat as constant but
> >gets passed something non-constant than there's no problem at
> >all.
> That's my confusion. The compiler complains when the function int
> foo(const char **ca) is passed a non-const pointer, char **c_array. I
> thought you could always go from non-const to const without any
> problems.
There's the following problem in your special case:
When you
int foo( const char ** p );
char **a;
f( a );
then the caller expects that it can change the array 'a' is
pointing to. But in foo() you can change what it points to
and it could be made to point to an immutable array. So
for example you could be trying to do
void foo( const ** p ) { *p = "hello"; }
int main( )
{
char *a;
foo( &a );
a[ 1 ] = 'x';
return 0;
}
So you make 'a' indirectly point to an (of course im-
mutable) string literal in foo() even though it's not
declared as a pointer to const char. And now there's
no protection anymore against changing what 'a' points
to in main(). But when you now try to modify it things
can go badly wrong. That should be excluded by using
void foo( const char * const * p );
since with that what 'p' points to also can't modified
in foo() (although my compiler still complains and I
haven't yet figured out why - or what what nasty things
I still could do in foo() ;-)
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
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