Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strtok and strtok_r

Reply
Thread Tools

strtok and strtok_r

 
 
Tor Rustad
Guest
Posts: n/a
 
      09-19-2007
On 17 Sep, 21:00, Richard Heathfield <(E-Mail Removed)> wrote:

[...]

> Kind of you, James, and I must admit I find it hard to imagine arguing that
> way as well. Unfortunately, I remember that I had some pretty strange
> misconceptions about C a decade or so ago, so it's not utterly impossible.
> But no, I don't remember this particular debate. Your mod seems plausible,
> however. (I can't think of any reason why I'd want to call memcpy in that
> way, however.)


I found the thread, it was more than 7 years ago.. you gave me that
lesson. Posting the link to the thread, would be rather humiliating
for me, so I prefer not to!

Here is the function R.H. wrote back then:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>


char *dupstr(char *s)
{
char *t = NULL;
assert(s != NULL);
if(s != NULL) /* assert won't fire if NDEBUG is defined, so check
anyway */
{
t = malloc(strlen(s) + 1); /* allocate enough bytes to make a
copy
of the string pointed to by s */
if(t != NULL) /* did it work? */
{
strcpy(t, s); /* perform the copy */
}
}
return t; /* returns NULL if the string could not be created */
}

and Ben Pfaff instantly suggested the "char *dupstr(const char *s)"
improvement.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strtok_r and delimiters Marco Trapanese C Programming 4 05-20-2008 07:55 PM
strtok and strtok_r siddhu C++ 6 09-15-2007 09:21 AM
what's wrong with this strtok_r() g C++ 4 10-30-2006 11:47 PM
strtok() and std::string Alex Vinokur C++ 6 04-14-2005 01:40 PM
strtok/strtok_r woes kbhat@kaxy.com C Programming 23 01-28-2005 08:55 PM



Advertisments