Glen Gray said:
> Looking for a simple way to cat two strings into a third.
> I had the following in my code
>> msg = strdup(strcat(strvar1, strvar2));
>> What happens here is the strvar2 is concatinated to strvar1 and a
> pointer to strvar1 is returned to strdup which allocates memeory for the
> char *message variable.
>> What I want is that functionality without strvar1 being modified. I
> would rather not have to create my own buffer and do it over two lines.
>> I thought this was interesting as I couldn't see and alternative to
> strcat. Hope someone else can.
Save yourself some pain, use snprintf.
snprintf (msg, sizeof(msg), "%s%s", strvar1, strvar2);
bonus: no need to worry about any security implications as well ;)
--j.
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!