public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Don't understand cv-quals message
@ 1998-02-01  4:28 Han Holl
  1998-02-02  2:38 ` Andreas Schwab
  1998-02-02 21:49 ` Joe Buck
  0 siblings, 2 replies; 3+ messages in thread
From: Han Holl @ 1998-02-01  4:28 UTC (permalink / raw)
  To: egcs

Hello,

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).

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).

Thanks in advance for any explanation,

Han Holl
	


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Don't understand cv-quals message
  1998-02-01  4:28 Don't understand cv-quals message Han Holl
@ 1998-02-02  2:38 ` Andreas Schwab
  1998-02-02 21:49 ` Joe Buck
  1 sibling, 0 replies; 3+ messages in thread
From: Andreas Schwab @ 1998-02-02  2:38 UTC (permalink / raw)
  To: Han Holl; +Cc: egcs

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Don't understand cv-quals message
  1998-02-01  4:28 Don't understand cv-quals message Han Holl
  1998-02-02  2:38 ` Andreas Schwab
@ 1998-02-02 21:49 ` Joe Buck
  1 sibling, 0 replies; 3+ messages in thread
From: Joe Buck @ 1998-02-02 21:49 UTC (permalink / raw)
  To: Han Holl; +Cc: egcs

> Hello,
> 
> 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).

Your code is broken, and gcc should reject it even in normal mode.
You can't convert a char** to a const char**.  It is not safe.

> 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.

No, there is no such promise here.

> (This works of course with passing char * to a const char * function).

It is not the same.  For a discussion, see the comp.std.c++ FAQ:

http://reality.sgi.com/employees/austern_mti/std-c++/faq.html

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1998-02-02 21:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-02-01  4:28 Don't understand cv-quals message Han Holl
1998-02-02  2:38 ` Andreas Schwab
1998-02-02 21:49 ` Joe Buck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).