Changes between Version 8 and Version 9 of const
- Timestamp:
- Jun 13, 2010, 4:31:39 AM (15 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
const
v8 v9 41 41 If you compile with `gcc -Wwrite-strings`, literal strings will be typed `const char[]` like they should be, to prevent you from making the mistake of storing one in a `char *`. 42 42 43 String functions in the standard C library accept `const char *` and return `char *`, again for compatibility; this does not cause problems because of the implicit conversion from `char *` to `const char *`. (Note that abuses such as `const char *p; char *q = strstr(p, "");` are still unsafe even though the y fail to be disallowed by the signature of `strstr`.) However, a few functions like `execv` that deal with ''pointers'' to strings (or arrays of strings) were forced to choose one or the other, and they take `char **` or `char *const *` rather than `const char *const *`. A cast is required to use these functions with `const char *` strings. glibc’s `getopt` is a [http://www.kernel.org/doc/man-pages/online/pages/man3/getopt.3.html#CONFORMING_TO particularly special case].43 String functions in the standard C library accept `const char *` and return `char *`, again for compatibility; this does not cause problems because of the implicit conversion from `char *` to `const char *`. (Note that abuses such as `const char *p; char *q = strstr(p, "");` are still unsafe even though the the signature of `strstr` fails to disallow them.) However, a few functions like `execv` that deal with ''pointers'' to strings (or arrays of strings) were forced to choose one or the other, and they take `char **` or `char *const *` rather than `const char *const *`. A cast is required to use these functions with `const char *` strings. glibc’s `getopt` is a [http://www.kernel.org/doc/man-pages/online/pages/man3/getopt.3.html#CONFORMING_TO particularly special case]. 44 44 45 45 == Using `const` effectively ==