If you don't want to change the strings, then you could simply do
(如果您不想更改字符串,那么您可以这样做)
const char *a[2];
a[0] = "blah";
a[1] = "hmm";
When you do it like this you will allocate an array of two pointers to const char
.
(当你这样做时,你将分配一个两个指向const char
指针的数组。)
These pointers will then be set to the addresses of the static strings "blah"
and "hmm"
. (然后将这些指针设置为静态字符串"blah"
和"hmm"
。)
If you do want to be able to change the actual string content, the you have to do something like
(如果您确实希望能够更改实际的字符串内容,则必须执行类似操作)
char a[2][14];
strcpy(a[0], "blah");
strcpy(a[1], "hmm");
This will allocate two consecutive arrays of 14 char
s each, after which the content of the static strings will be copied into them.
(这将分配两个连续的14个char
的数组,之后静态字符串的内容将被复制到它们中。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…