From mboxrd@z Thu Jan 1 00:00:00 1970 From: Andreas Schwab To: "Han Holl" Cc: egcs@cygnus.com Subject: Re: Don't understand cv-quals message Date: Mon, 02 Feb 1998 02:38:00 -0000 Message-id: References: <19980201122701.23062.qmail@optiplex.palga.uucp> X-SW-Source: 1998-02/msg00067.html Han Holl writes: |> If I compile the following program (with egcs-1.0.1) : |> #include |> void f(const char** s) |> { |> cout << *s << endl; |> } |> main() |> { |> char *cs = "abcd"; |> f(&cs); |> } |> with: |> g++ --pedantic |> I get the following warning: |> t1.C: In function `int main()': |> t1.C:11: warning: passing `char **' as argument 1 of `f(const char **)' \ |> adds cv-quals without intervening `const' |> What does this mean, and can I do anything to make it go away? (I.e., |> can I make this code even more 'pedanticly' correct ? I fail to see how). Declare cs as const char *, because that is what it really is (since you are assigning it a constant string). |> As I understand this, what f() does is promise not to poke around in the |> characters, and I should be able to pass a non-const char ** to it. |> (This works of course with passing char * to a const char * function). If you change f as follows you'll see the problem: void f(const char **s) { static const char f_s1[] = "asdf"; *s = f_s1; // OK } main() { static char main_s1[] = "asdf"; // make it writable char *cs = main_s1; // OK *cs = 'b'; // OK f(&cs); // BAD *cs = 'b'; // Overwriting constant f_s1! } See also the comp.lang.c FAQ. -- Andreas Schwab "And now for something schwab@issan.informatik.uni-dortmund.de completely different" schwab@gnu.org