Don't understand cv-quals message
Andreas Schwab
schwab@issan.informatik.uni-dortmund.de
Mon Feb 2 02:38:00 GMT 1998
Han Holl <jeholl@euronet.nl> writes:
|> If I compile the following program (with egcs-1.0.1) :
|> #include <iostream.h>
|> 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
More information about the Gcc
mailing list