From: Dave Neary (dneary at domain eircom.net)
Date: Wed 02 May 2001 - 21:53:34 IST
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 domain eircom.net | \ Phone +353-1-872-0654 / `------------------------------'
This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:10:09 GMT