Is this a valid ANSI program?

Andrew Koenig ark at alice.att.com
Thu Jun 27 04:41:19 AEST 1991


In article <609 at mtndew.Tustin.CA.US> friedl at mtndew.Tustin.CA.US (Stephen J. Friedl) writes:

> 	void foo(const char **xxx) { }

> 	main()
> 	{
> 	char	**p = 0;

> 		foo(p);
> 	}

> The compiler claims that the argument /p/ to the function foo()
> is incompatible with the prototype, and I just don't believe it.

The compiler is correct: you cannot convert char ** to const char ** .
To see why, consider the following program fragment:

	main()
	{
		const char victim = '?';
		char *accomplice;
		const char **sneak = &accomplice;	/* */
		*sneak = &victim;
		*accomplice = '!';
	}

If char ** could be converted to const char **, the line with the
comment would be legal.  None of the other lines is the least bit
controversial; the declarations are surely beyond reproach,
the assignment to *sneak places the address of a "const char"
into a "const char *", and the assignment to *accomplice
assigns an "int" to a "char".  Thus allowing the commented line
would open a hole in the type system by allowing you to modify
a "const char" object without an explicit cast.
-- 
				--Andrew Koenig
				  ark at europa.att.com



More information about the Comp.lang.c mailing list