On Tue, Jul 03, 2001 at 05:07:27AM -0400 or thereabouts, kevin lyda wrote:
>> well, i left some thoughts out. all the str* routines assume strings are
> nul (not NULL) terminated. a more dynamic version would malloc a char *
> of strlen(s1) + strlen(s2) + 1;
>> On Tue, Jul 03, 2001 at 08:38:50AM +0000, Conor Daly wrote:
> > 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()?
>> so how is it defined? if it's
>> struct foo {
> char s[123];
> };
>> then no, the memory is already allocated. in fact using strdup2 would
> be an error. otoh, if it is defined as:
>> struct foo {
> char *s;
> };
>> then the answer is "it depends." if that element of the struct is already
> pointing at a string, you could use strncpy (if you know the length of
> the data pointed to by s). or you could free it and use strdup2.
"if you know the length of the data pointed to by s"
Therin lies the rub. I'm trying to be general here so I don't know the
length of the destination char *array* 's'. I can find out the length of the
string *within* 's' using strlen() but I don't know of a function that will
return the amount of memory *allocated* to 's'.
> or you could free it and use strdup2.
This looks likely. so I use
foo.s = mystrdup2(foo.s, s2);
where
char * mystrdup2(char *s1, char *s2) {
/* create a tmp string to hold 's1' while we free that */
char *tmp;
/* take a copy of s1 */
if( (tmp = strdup(s1)) == NULL) return(s1); /* we failed to make a copy so bail out */
/* then free it */
free(s1);
/* now use strdup2() to concat the two strings */
if( (s1 = strdup2(tmp, s2)) == NULL) {
/* the copy failed so we want to return the original string unchanged.
* Maybe we should think about some way for the caller to be told that
* we couldn't do the requested job but if we return NULL, we've
* destroyed the original string which might upset the user!
*/
return(tmp);
} else {
/* free the tmp string */
free(tmp);
/* and return the new pointer to s1 */
return(s1);
}
}
All this depends on being able to do malloc and free stuff with bits of
structs. Like:
struct foo {
char *s;
} foozle; /* to plageri[sz]e a term of Kevin's)
foozle.s = (char *) malloc( <number of chars required> * sizeof(char));
and
free(foozle.s);
Conor
--
Conor Daly
Met Eireann, Glasnevin Hill, Dublin 9, Ireland
Ph +353 1 8064276 Fax +353 1 8064275
------------------------------------
9:14am up 39 days, 20:17, 14 users, load average: 2.02, 2.02, 2.00
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!