LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] C string concat question revisited

[ILUG] C string concat question revisited

Conor Daly conor.daly at met.ie
Tue Jul 3 09:38:21 IST 2001


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




More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


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!
RSS Version
Powered by Dell