[ILUG] C string concat question ??

Conor Daly conor.daly at met.ie
Thu May 3 10:20:06 IST 2001


On Wed, May 02, 2001 at 06:09:29PM +0100 or thereabouts, Kenn Humborg wrote:
>    msg = malloc(strlen(strvar1) + strlen(strvar2) + 1);
>    if (msg == NULL) {
>        /* argh */
>    } else {
>       strcpy(msg, strvar1);
>       strcat(msg, strvar2);
>    }

and if you've a variable number of variables to concat like argv[*] you
could go with

MSG_SIZE=1;
for (i=0; i<argc; i++) {
  MSG_SIZE += strlen(argv[i]);
}

msg = malloc(MSG_SIZE);

I was just trying to reimplement "watch" for a solaris box and that bit was
puzzling me.

Conor
-- 
Conor Daly 
Met Eireann, Glasnevin Hill, Dublin 9, Ireland
Ph +353 1 8064217 Fax +353 1 8064275
------------------------------------
  9:16am  up 23 days, 18:27,  5 users,  load average: 1.01, 1.01, 1.00




More information about the ILUG mailing list