更新时间:2023-11-14 12:14:28
此段错误是因为您缺少while
周围的括号.您将继续打印测试1",直到strsep
返回NULL
,然后尝试打印该结果(和段错误).
The segfault is because you're missing braces around your while
. You'll keep printing "Test 1" until strsep
returns NULL
, then you try to print that result (and segfault).
带有几个警告标记(可能是-Wall
),gcc可以在这里提供帮助:
With several warning flags (probably -Wall
), gcc helps out here:
sep.c:13:3: warning: this ‘while’ clause does not guard... [-Wmisleading-indentation]
while((found = strsep(&cp,"/,-")) != NULL )
^~~~~
sep.c:15:5: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘while’
printf("%s\n",found);
^~~~~~
在while
周围添加花括号,该程序将按预期工作:
With braces added around the while
, the program works as expected:
./sep
Enter string: abc/def
Original string: 'abc/def'
Test 1abc
Test 1def