J
Jonathan Burd
Greetings everyone,
I wrote a function to learn about variable-length argument lists. I
wonder if there is a better way to detect the end of the argument list
than using a sentinel value like the one I am using (NULL in this
example) or an argument count parameter (ugh).
The following function concatenates a series of C-style strings. I am
aware of the fact that not allocating enough memory for `dst` results
in UB (gave me a segmentation fault).
#include <stdarg.h>
char *
append (
char * dst, // destination string buffer
const char * base, // first string
const char * append, // second string
/* const char * appN */ ... // n strings
/* , NULL */ // sentinel null indicating list end
)
{
va_list ap;
char * eosdst = dst;
const char * cp = NULL;
/* copy base to dst */
while ((*eosdst = *base))
++eosdst, ++base;
/* copy append to dst */
while ((*eosdst = *append))
++eosdst, ++append;
/* copy remaining strings if they exist. NULL -- terminate */
va_start (ap, append);
while (NULL != (cp = va_arg (ap, const char*)))
while ((*eosdst = *cp))
++eosdst, ++cp;
va_end (ap);
return (dst);
}
Sample Usage:
#include <stdio.h>
/* ... */
int
main (int argc, char ** pp_argv)
{
char buf[200];
char * tst = "test";
char * teststring2 = "This is some more text.\nSee if this works
well.";
append (buf, "--d--", teststring2, teststring2, tst, "\n", NULL);
printf (buf);
return (0);
}
Any help will be appreciated. If you have suggestions, please feel free
to comment so I can improve. Thank you.
Regards,
JB
I wrote a function to learn about variable-length argument lists. I
wonder if there is a better way to detect the end of the argument list
than using a sentinel value like the one I am using (NULL in this
example) or an argument count parameter (ugh).
The following function concatenates a series of C-style strings. I am
aware of the fact that not allocating enough memory for `dst` results
in UB (gave me a segmentation fault).
#include <stdarg.h>
char *
append (
char * dst, // destination string buffer
const char * base, // first string
const char * append, // second string
/* const char * appN */ ... // n strings
/* , NULL */ // sentinel null indicating list end
)
{
va_list ap;
char * eosdst = dst;
const char * cp = NULL;
/* copy base to dst */
while ((*eosdst = *base))
++eosdst, ++base;
/* copy append to dst */
while ((*eosdst = *append))
++eosdst, ++append;
/* copy remaining strings if they exist. NULL -- terminate */
va_start (ap, append);
while (NULL != (cp = va_arg (ap, const char*)))
while ((*eosdst = *cp))
++eosdst, ++cp;
va_end (ap);
return (dst);
}
Sample Usage:
#include <stdio.h>
/* ... */
int
main (int argc, char ** pp_argv)
{
char buf[200];
char * tst = "test";
char * teststring2 = "This is some more text.\nSee if this works
well.";
append (buf, "--d--", teststring2, teststring2, tst, "\n", NULL);
printf (buf);
return (0);
}
Any help will be appreciated. If you have suggestions, please feel free
to comment so I can improve. Thank you.
Regards,
JB