【发布时间】:2013-09-19 12:34:11
【问题描述】:
在此代码中 sn-p:
printf("shell> ");
fgets(input, MAX_INPUT_SIZE, stdin);
//tokenize input string, put each token into an array
char *space;
space = strtok(input, " ");
tokens[0] = space;
int i = 1;
while (space != NULL) {
space = strtok(NULL, " ");
tokens[i] = space;
++i;
}
//copy tokens after first one into string
strcpy((char*)cmdargs, ("%s ",tokens[1]));
for (i = 2; tokens[i] != NULL; i++) {
strcat((char*)cmdargs, ("%s ", tokens[i]));
}
printf((char*)cmdargs);
输入:echo hello world and stuff,程序打印:
helloworldandstuff
在我看来,strcat((char*)cmdargs, ("%s ", tokens[i])); 行应该将 tokens[i] 处的字符串与后面的空格连接起来。 strcat 不适用于字符串格式吗?任何其他想法可能会发生什么?
【问题讨论】:
-
是的,它编译在这里就好了。关于
printf((char*)cmdargs);声明的一个警告,仅此而已。该语句仅用于调试目的。 -
它会编译因为逗号运算符,而只是
"%s ", tokens[i]不像你想的那样有效