其中之一,你不能做你的问题,仍然有他们作为空终止的字符串工作。这是因为text
内存布局是这样的:
char text_as_array[] = {
'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '.', '\0'
};
通知的'\0'
底。字符数组test
实际上不是长度12,它是长度。
空终止所需的控制台输出函数,如printf和std ::法院(C++)的工作:
char good[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
printf("%s\n", good); /* this works, because it is null-terminated */
char bad[] = { 'H', 'e', 'l', 'l', };
printf("%s\n", bad); /* this will print out garbage, or crash your program! */
这意味着你必须定义你这样的数组:
char one[6];
char two[3];
char three[6];
如果你想少做typi
one[0] = text[7]; /* o */
one[1] = text[8]; /* r */
one[2] = text[9]; /* l */
one[3] = text[10]; /* d */
one[4] = text[11]; /* . */
one[5] = '\0'; /* you have to null-terminate the string */
:
您可以通过手来简单地复制值超出做到这一点NG,或只是想写出更好的代码,你可以利用一个事实,即数组/串是连续的优势,并使用一个循环来将数据复制过来:
for(int i = 0; i < 5; ++i)
{
one[i] = text[7 + i];
}
one[5] = '\0';
但是,如果你想这是一个非常C中常见的事情,那么你猜对了。而不是每次手动编码此循环,您应该使用内置函数为您进行复制:
/* C uses "", C++ uses <> */
#include "string.h" /* C++: #include<cstring> */
#include "stdio.h" /* C++: #include<cstdio> */
/* don't do "int function(void)", do "int function()" instead */
int main()
{
char *text = "Hello World."; /* string length 12, array size 13 */
/* size of these has to accomodate the terminating null */
char one[6];
char two[3];
char three[6];
/* copy two characters, starting at index 5 */
strncpy(two, &text[5], 2);
/* for three, we don't have to do &text[0], because it is at the beginning of the string */
strncpy(three, text, 5);
/* we can do strcpy if we're at the end of the string. It will stop when it hits '\0' */
strcpy(one, &text[7]);
/* note that we still have to null-terminate the strings when we use strncpy */
two[2] = '\0';
three[5] = '\0';
/* but we don't have to null-terminate when we use strcpy */
/* so we can comment this out: one[5] = '\0'; */
printf("%s\n", one);
printf("%s\n", two);
printf("%s\n", three);
return 0; /* returns 0, since no errors occurred */
}
这没有任何意义。 “将问候分成三个单词”意味着什么? – bmargulies 2010-10-17 22:48:38
这应该是一个通用的答案,或只为“Hello World”?你已经澄清了你的问题,因为现在它是模糊的,可能是功课。 – birryree 2010-10-17 22:51:45
为了减少混淆,我编辑了这个问题,但仍然完全符合他原先的意图(密切关注他所调用的字符串,为什么他会有* 3个变量,如果他显然只有两个单词?)。其他编辑改变了问题的含义,答案不会是他想要的。 – 2010-10-17 23:26:16