This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: constification for lang_specific_driver
- To: gcc-patches at gcc dot gnu dot org
- Subject: Re: constification for lang_specific_driver
- From: "Joseph S. Myers" <jsm28 at cam dot ac dot uk>
- Date: Thu, 3 Aug 2000 01:08:16 +0100 (BST)
Zack Weinberg wrote:
> Does anyone know why the second argument of execvp() is declared as
> char * const *, instead of const char *const * ?
So that a char ** can be passed as an argument.
See B.3.1.2 in POSIX.1:1996; the following text is pasted from the latest
Austin Group draft and seems much the same.
The statement about argv[] and envp[] being constants is included
to make explicit to future writers of language bindings that these
objects are completely constant. Due to a limitation of the ISO C
standard, it is not possible to state that idea in standard C.
Specifying two levels of const-qualification for the argv[] and
envp[] parameters for the exec functions may seem to be the
natural choice, given that these functions do not modify either
the array of pointers or the characters to which the function
points, but this would disallow existing correct code. Instead,
only the array of pointers is noted as constant. The table of
assignment compatibility for dst=src, derived from the ISO C
standard summarizes the compatibility:
dst: char *[] const char *[] char *const[] const char *const[]
src:
char *[] VALID - VALID -
const char *[] - VALID - VALID
char * const [] - - VALID -
const char *const[] - - - VALID
Since all existing code has a source type matching the first row,
the column that gives the most valid combinations is the third
column. The only other possibility is the fourth column, but
using it would require a cast on the argv or envp arguments. It
is unfortunate that the fourth column cannot be used, because the
declaration a non-expert would naturally use would be that in the
second row.
(The existing code for these purposes being pre-standard C knowing nothing
of const.)
--
Joseph S. Myers
jsm28@cam.ac.uk