On Wed, May 02, 2001 at 05:58:59PM +0100, Glen Gray wrote:
> 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.
>> Glen
After reading Kenn's solution, I have to say I agree with him :)
Stretch it out! But if you wanted the short way, you could always
(after properly allocating space for msg) do
msg=strdup(strvar1),
(msg = realloc(msg, strlen(strvar1) + strlen(strvar2) +
1))==NULL? realloc_failed_stuff(__FILE__, __LINE__): 1,
strcat(msg, strvar2);
Of course, there's nothing stopping you from putting the whole
program on one line using the comma operator. As long as you
don't use if, while or for statements :)
And my first instinct...
strcat(msg=strdup(strvar1),strvar2);
doesn't work because you can never be sure what order the
arguments will evaluate. So strcat might cat strvar2 on the
(uninitialised) msg before strdup over-writes it with strvar1.
Cheers,
Dave.
--
.------------------------------.
/ David Neary, \
| E-Mail dneary at eircom.net |
\ Phone +353-1-872-0654 /
`------------------------------'
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!