- 论坛徽章:
- 95
|
原帖由 antigloss 于 2007-4-27 19:14 发表
string literals 是常量。正确的用法是
const char *p = "hello world!";
之所以还允许楼主那样写,是为了和过去兼容
楼主的用法倒也算不上错误。当然 antigloss 给出的用法更安全些。
引述一下 comp.lang.c FAQ Question 1.32 的说法:
A string literal (the formal term for a double-quoted string in C source) can be used in two slightly different ways:
1. As the initializer for an array of char, as in the declaration of char a[] , it specifies the initial values of the characters in that array (and, if necessary, its size).
2. Anywhere else, it turns into an unnamed, static array of characters, and this unnamed array may be stored in read-only memory, and which therefore cannot necessarily be modified. In an expression context, the array is converted at once to a pointer, as usual (see section 6), so the second declaration initializes p to point to the unnamed array's first element. |
评分
-
查看全部评分
|