char** and const char** ... 
Author Message
 char** and const char** ...

Hi! I am trying to pass a char** to a function which is not expected
to modify it. It just refers to the variable. So, the called function
is of the form:

int some_function (const char ** pp_c)
{
    /* some code that does not modify pp_c, just uses its contents
     * to do its work.
     */

    return (result);

Quote:
}

int some_other_function (/* various parameters */)
{
    int result;
    char ** pp_c;

    /* some code not relevant */

    /* code that allocates and fills in pp_c based on passed in
parameters */

    result = some_function (pp_c); /* !!!!!!!! */

    return (result);

Quote:
}

This generates a compiler warning at the line marked with "!"
character.

Any idea why?

I am writing this code on a RH8.0 linux system (gcc version 3.2). But,
more experimentation showed the same problem repeat on RH7.3 with
older (gcc version 2.96) compiler!

Help! I need to get this done soon.

Thanks!
--



Mon, 22 Aug 2005 13:51:12 GMT  
 char** and const char** ...


Quote:
>Hi! I am trying to pass a char** to a function which is not expected
>to modify it. It just refers to the variable. So, the called function
>is of the form:

>int some_function (const char ** pp_c)
>{
>    /* some code that does not modify pp_c, just uses its contents
>     * to do its work.
>     */

>    return (result);
>}

>int some_other_function (/* various parameters */)
>{
>    int result;
>    char ** pp_c;

>    /* some code not relevant */

>    /* code that allocates and fills in pp_c based on passed in
>parameters */

>    result = some_function (pp_c); /* !!!!!!!! */

>    return (result);
>}

>This generates a compiler warning at the line marked with "!"
>character.

>Any idea why?

It would be nice of you to tell us what the warning says.

I imagine it has to do with an attempt to pass the wrong type of
pointer.

The language can convert a (T *) to a (const T *), but it won't go
deeper than that.  Specifically, it can convert (char **) to
(char * const *), but it can't convert (char **) to a (const char **).

If you can't change the function signature, you can use a cast to do
the conversion:

     result = some_function ( (const char **)pp_c );

Quote:
>I am writing this code on a RH8.0 linux system (gcc version 3.2). But,
>more experimentation showed the same problem repeat on RH7.3 with
>older (gcc version 2.96) compiler!

>Help! I need to get this done soon.

--



Tue, 23 Aug 2005 02:55:32 GMT  
 char** and const char** ...

Quote:

> int some_function (const char ** pp_c)
> {
>     /* some code that does not modify pp_c, just uses its contents
>      * to do its work.
>      */

The comment disagrees with the declaration.  You have said
in the declaration that the *char objects* will not be modified.
There are two levels of indirection which you have not
const-qualified.  It it is truly pp_c that is not going to
be modified, you could say char ** const pp_c.  If *pp_c
is going to be used to read but not write, try
char * const * pp_c.
--



Tue, 23 Aug 2005 02:55:39 GMT  
 char** and const char** ...

|> Hi! I am trying to pass a char** to a function which is not expected
|> to modify it. It just refers to the variable. So, the called function
|> is of the form:
|>
|> int some_function (const char ** pp_c)
|> {
|>     /* some code that does not modify pp_c, just uses its contents
|>      * to do its work.
|>      */
|>
|>     return (result);
|> }
|>
|> int some_other_function (/* various parameters */)
|> {
|>     int result;
|>     char ** pp_c;
|>
|>     /* some code not relevant */
|>
|>     /* code that allocates and fills in pp_c based on passed in
|> parameters */
|>
|>     result = some_function (pp_c); /* !!!!!!!! */
|>
|>     return (result);
|> }
|>
|> This generates a compiler warning at the line marked with "!"
|> character.
|>
|> Any idea why?

Consider extending your example like this:

int some_function (const char ** pp_c)
{
    static const char readonly_memory[] = "...";

    *pp_c = readonly_memory; /* OK */
    return 1;

Quote:
}

int some_other_function ()
{
    int result;
    char *pp;
    char ** pp_c = &pp;

    result = some_function (pp_c); /* Assume this is OK */

    *pp = 'X';  /* BAD!  Modifying readonly_memory! */

    return (result);

Quote:
}

Andreas.

--

SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nrnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
--



Tue, 23 Aug 2005 02:55:42 GMT  
 char** and const char** ...
Try with

int some_function (char ** const pp_c)

instead of

int some_function (const  char ** pp_c)

You are passing a constant pointer , not a pointer to a constant.

"When modifying a data declaration, the const keyword specifies that the
object or variable is not modifiable. When following a member function's
parameter list, the const keyword specifies that the function doesn't modify
the object for which it is invoked" , Microsoft says

Paolo
--



Tue, 23 Aug 2005 02:55:49 GMT  
 char** and const char** ...

Quote:

> Any idea why?

This was new to me too. Take a look at message id

Via google groups:
http://groups.google.com/groups?&selm=fa.q66kr2v.1j5oijp%40ifi.uio.no

Krister Walfridsson wrote 1999/04/11 in fa.netbsd.current-users:

Quote:
> So "const char **" denotes a pointer to an unqualified type. Its type
> is "a pointer to a pointer to a qualified type". So "char **" and
> "const char **" are both pointers to unqualified types that are not
> the same type, so they are not compatible types, and the constraint
> in Section 6.3.2.2 is violated, and a diagnostic message must be
> produced.

I am pretty sure this will help you.

Regards,
Sven

--
Sven Semmler                   http://www.semmlerconsulting.com
fingerprint: 72CA E26D C2A3 1FEB 7AFC  10EA F769 A9A4 937F 5E67
--



Tue, 23 Aug 2005 02:55:51 GMT  
 char** and const char** ...

Quote:

> This generates a compiler warning at the line marked with "!"
> character.

*What* warning?

--

Even if all the snow were burnt, ashes would remain.
--



Tue, 23 Aug 2005 02:56:09 GMT  
 char** and const char** ...


Quote:
>Hi! I am trying to pass a char** to a function which is not expected
>to modify it. It just refers to the variable. So, the called function
>is of the form:

>int some_function (const char ** pp_c)
[...]
>    char ** pp_c;
[...]
>    result = some_function (pp_c); /* !!!!!!!! */

See http://www.eskimo.com/~scs/C-faq/q11.10.html

Regards,

                               -=Dave
--
Change is inevitable, progress is not.
--



Tue, 23 Aug 2005 02:56:13 GMT  
 char** and const char** ...

Quote:
> Any idea why?

> I am writing this code on a RH8.0 linux system (gcc version 3.2). But,
> more experimentation showed the same problem repeat on RH7.3 with
> older (gcc version 2.96) compiler!

It's hard to see that compilers have no intelligence like human,though
logically there is no problem in doing like this but programtically
it's an "warning" :: in the language of compiler "warning" is
issued to warn you of probable "unconcious mistakes" made by you.

Solution:-
   If you think that your program is correct, then you can get rid
of this warning by changing your code as,

    result = some_function ((const char **)pp_c); /* !!!!!!!! */
--



Thu, 25 Aug 2005 16:57:41 GMT  
 char** and const char** ...


Quote:
>This generates a compiler warning at the line marked with "!"
>character.

>Any idea why?

Because it is unsafe:
void modify(Foo *);

 int main()
 {
   const Foo x;
   Foo* p;
   const Foo** q = &p;  // q now points to p; this is (fortunately!) an
error
   *q = &x;             // p now points to x
   modify(p);         // Ouch: modifies a const Foo!!
   ...
 }

This problem is well known to the more expert C++ programmer where const
correctness is considered important but is just as true for C.

Quote:

>I am writing this code on a RH8.0 linux system (gcc version 3.2). But,
>more experimentation showed the same problem repeat on RH7.3 with
>older (gcc version 2.96) compiler!

>Help! I need to get this done soon.

--
ACCU Spring Conference 2003 April 2-5
The Conference you cannot afford to miss
Check the details: http://www.accuconference.co.uk/
Francis Glassborow      ACCU
--



Thu, 25 Aug 2005 18:07:09 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. char^ to const char* casting

2. convert char to const char

3. what's the difference between char * and const char *

4. char *strchr(const char *, int) ??!!!

5. char *fnpars(const char *fn, int p, int v)

6. Help: inline char const* const& max(char const* const &a, char const* const &b)

7. const char * vs char const *

8. const char *p == char const *p ?

9. Help - encryption and "strstr(const char *1, const char *2)" problems...

10. va_arg(ap, const char *) = char*?

11. const char* to char[64]

12. const char ** incompatible with char ** (and vice versa)?

 

 
Powered by phpBB® Forum Software