man 3 getopt char * const argv[] - is const wrong ?

Matthias Andree mandree at FreeBSD.org
Sun Feb 13 13:23:05 UTC 2011


Am So, 13.02.2011, 13:20 schrieb Julian H. Stacey:
> Hi Hackers
> Ref.: man 3 getopt
> 	int getopt(int argc, char * const argv[], const char *optstring);
> Ref.: K&R 2nd Ed P.211 last indent, 2nd sentence
> 	The purpose of const is to announce [objects] that may be
> 	placed in read-only memory, and perhaps to increas[e] opportunities for
optimization
> optstring is obviously const,
> but I don't see that argv can calim to be const ?

Hi Julian,

the prototype is in line with the Single Unix Specification v4 aka IEEE
Std. 1003.1-2008 (sorry no URL, I have checked my local copy, check
<http://www.opengroup.org/> you can access it free of charge after
registering name and email address).

The const basically states that the argv[] elements (i. e. the char *
pointers) are const[ant], or, read another way, that getopt promises not
to mess with the *pointers* but is free to modify the actual strings
pointed to (with the usual constraints of not assuming they can be
extended, for instance) - and I don't see the problem in that.

Does that help?  If not, please explain you confusion in a bit more detail.

Best
Matthias

-- 
Matthias Andree




More information about the freebsd-hackers mailing list