<分区>
我尝试构建并运行以下程序,但它无法执行。我以为我可能犯了一个错误,但显示了 0 个错误和 0 个警告。
在研究了 stackoverflow 上的此类行为后,我主要看到一些放错地方的分号或忘记的地址运算符,我在这个源代码中没有看到这些,或者我忽略了什么? 某些 C 或 GCC 大师可以告诉我哪里出了问题以及为什么吗?
操作系统是Windows 7,并且编译器已经启用: -pedantic -w -Wextra -Wall -ansi
这是源代码:
#include <stdio.h>
#include <string.h>
char *split(char * wort, char c)
{
int i = 0;
while (wort[i] != c && wort[i] != '\0') {
++i;
}
if (wort[i] == c) {
wort[i] = '\0';
return &wort[i+1];
} else {
return NULL;
}
}
int main()
{
char *in = "Some text here";
char *rest;
rest = split(in,' ');
if (rest == NULL) {
printf("\nString could not be devided!");
return 1;
}
printf("\nErster Teil: ");
puts(in);
printf("\nRest: ");
puts(rest);
return 0;
}
预期的行为是字符串“Some text here”在其第一个空格处拆分,预期的输出为:
Erster Teil: Some
Rest: text here