Re: [ILUG] C string concat question ??

From: Sean McGrath - Sun Microsystems Ireland (sean.mcgrath at domain sun.com)
Date: Thu 03 May 2001 - 10:50:21 IST


Kenn Humborg stated:
< > 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);
<
< Sigh...
<
< msg = malloc(MSG_SIZE+1);

  err, isn't that why hes set MSG_SIZE=1; at the start ????

<
< Later,
< Kenn
<
<
< --
< Irish Linux Users' Group: ilug at domain linux.ie
< http://www.linux.ie/mailman/listinfo/ilug for (un)subscription information.
< List maintainer: listmaster at domain linux.ie

-- 
Sean.
.
At the mo its Incognito Pieces Of A Dream


This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:10:10 GMT