I'm writing a parser in C, and I've got some code that looks like this:
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;
}
But I'm kinda new to the whole string manipulation aspect of C, so how would I append the character returned from the function consumed_char(self)
to the result
char pointer? I've seen people using the strcat
function, but that wont work as it takes two constant char pointers, but I'm dealing with a char* and a char. In java it would be something like this:
result += consumed_char(self);
What's the equivalent in C? Thanks :)
In C, strings do not exist as a type, they are just char
arrays with a null-terminating character. This means, assuming your buffers are big enough and filled with zeroes, it can be as simple as:
result[(strlen(result)] = consumed_char(self);
if not, your best bet is to use strcat
and change your consumed_self
function to return a char *
.
That being said, writing a parser without basic understanding of C-style strings, is, to say the least, quite ambitious.