Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on How can I manage multiple consecutive strings in a buffer (and add more later)?
Post
How can I manage multiple consecutive strings in a buffer (and add more later)?
This question is inspired by If I have a char array containing strings with a null byte (\0) terminating each string, how would I add another string onto the end? on Stack Overflow.
Suppose I have a char[]
buffer that I'm using to represent multiple null-terminated (ASCII) strings, one after the other. I can easily set up an initial state that has two strings and sufficient room to add a third:
/* The exact amount of space is not critical to the question; it's enough
to store these strings and leave room for more. */
char buffer[80] = {'o', 'n', 'e', '\0', 't', 'w', 'o', '\0'};
Now suppose I have char* another_string = "three";
. How can I append or concatenate another_string
to the buffer
, generally? I do not want to concatenate the three
text with the two
, but instead put it in the buffer as a separate string.
I already know that the <string.h>
library functions expect a string to be null-terminated, so it seems like they won't help here. For example, strcat
would find the first null in the array instead of the second, and overwrite it; and strncpy
would need a pointer to where to start writing.
1 comment thread