[ILUG] C string concat question revisited

From: Conor Daly (conor.daly at domain met.ie)
Date: Tue 03 Jul 2001 - 09:38:21 IST


On Fri, May 04, 2001 at 01:11:07PM +0100 or thereabouts, kevin lyda wrote:
>
> #define STRSIZE 80
> char *
> strdup2(char *s1, char *s2)
> {
> char *s;
>
> if ((s=(char *)malloc(STRSIZE + 1)) == NULL) {
> return NULL;
> }
> /* the following initialises the string. s[0]=0;s[STRSIZE]=0; *
> * would work too. */
> memset(s, 0, STRSIZE + 1);
> /* from here on in we don't let str* routines near s[STRSIZE] *
> * so we know it will stay 0. */
> strncpy(s, s1, STRSIZE);
> strncat(s, s2, STRSIZE - strlen(s));
> return s;
> }
>
> strlen works by counting chars until it reaches '\0'. therefore don't use
> it if you're not sure the string is null terminated.
> sizeof(char) is not required.
>
> kevin

Some while back, Kevin posted the above which allocates *new* memory for a
string s which contains s1 and s2 (useage: s = strdup2(s1, s2); ).
Now, I have a string struct.s which, as the name suggests, is part of an
already allocated struct. If I want to set *that* to contain s1 and s2, can
I use the above ( struct.s = strdup2(s1, s2); ) or do I just use strncpy()
and strncat()?

Conor

-- 
Conor Daly 
Met Eireann, Glasnevin Hill, Dublin 9, Ireland
Ph +353 1 8064276 Fax +353 1 8064275
------------------------------------
  8:33am  up 39 days, 19:35, 14 users,  load average: 2.09, 2.08, 2.02


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