【发布时间】:2016-01-28 11:30:54
【问题描述】:
我尝试构建并运行以下程序,但它无法执行。我想也许我犯了一个错误,但显示了 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
【问题讨论】:
-
char* in = "Some text here";不会为您的字符串分配内存。它只分配内存来保存 char 指针。
标签: c pointers gcc compiler-errors compiler-warnings