【发布时间】:2015-01-04 06:42:26
【问题描述】:
我正在用 C 编写一个解析器,我有一些看起来像这样的代码:
char *consume_while(parser *self, int(*test)(char)) {
char *result;
while (eof(self) && (*test)(next_char(self))) {
// append the return value from the function consumed_char(self)
// onto the "result" string defined above.
}
return result;
}
但我对 C 的整个字符串操作方面有点陌生,所以我如何将从函数 consumed_char(self) 返回的字符附加到 result 字符指针?我见过有人使用strcat 函数,但这不起作用,因为它需要两个常量 char 指针,但我正在处理一个 char* 和一个 char。在java中它会是这样的:
result += consumed_char(self);
C 中的等价物是什么? 谢谢:)
【问题讨论】:
-
把
(*test)(next_char(self))改成test(next_char(self))会更容易阅读。 -
为小费干杯! :)
标签: c string pointers dynamic-allocation