another newbie question: strncpy function 
Author Message
 another newbie question: strncpy function

Quote:

>Now I'm trying to write my own version of strncpy; is there a more
>efficient way to do it than this: char *strncpy(char *s1, const char *s2,
>int n) {  int x;  for(x=0; x<n; x++)  {  if(*ptr2)  s1[x]=*s2++;  else
>s1[x]='\0';  }      return s1; } it seems to work, but it seems like it could
>be better...

WHile I don't know what ptr2 does, you might try putting the test for end
of the first string into the for loop's termination condition. Or add a
'break' statement to your if. They both accomplish the same thing; it is
primarily a style issue.

Off the top of my head, I have to admit I'd probably do something
semi-illegible like:

char *strncpy( char *s1, const char *s2, int n)
{
   int x;

   for ( x = 0; ( x < n) && *s2;)
      *s1++ = *s2++;

   if ( x < n)
      *s1 = 0;

   return s1;

Quote:
}

(And, yes, it's possible to fold my 'if' statement into the loop, but I
don't feel it would look very good, nor would it gain you much).

  Bob
--
  Bob           One good thing about being wrong is the joy it brings to others



Tue, 01 Aug 2000 03:00:00 GMT  
 another newbie question: strncpy function


Quote:

>>char *strncpy(char *s1, const char *s2, int n) { int x; for(x=0; x<n;
>>x++) { if(*ptr2) s1[x]=*s2++; else s1[x]='\0'; } return s1; }
>>it seems to work, but it seems like it could be better...

As Robert mentioned, if(*ptr2) looks fishy.  How did this function manage to
work?  You probably meant if(*s2) instead.

Quote:
>char *strncpy( char *s1, const char *s2, int n)
>{
>   int x;

>   for ( x = 0; ( x < n) && *s2;)
>      *s1++ = *s2++;

>   if ( x < n)
>      *s1 = 0;

>   return s1;
>}

>(And, yes, it's possible to fold my 'if' statement into the loop, but I
>don't feel it would look very good, nor would it gain you much).

Except that, now, it's not a valid implementation of strncpy().

#include <stddef.h>

char * my_strncpy(char *s1, const char *s2, size_t n)
{
   while (n--) *s1++ = (*s2) ? (*s2++) : (*s2);
   return s1;

Quote:
}

would be the implementation of my choice, but it shouldn't be
significantly faster (or slower) than the (corrected) version of the
function from the original post.  Any attempt to do loop unrolling,
etc., would be classified as micro-optimization, which probably wouldn't
help, and if it did, the optimization wouldn't be portable to all
platforms anyway.

So, my advice is, if the version you have works correctly, stick with it.

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>



Tue, 01 Aug 2000 03:00:00 GMT  
 another newbie question: strncpy function

Bob...

I think it might be helpful either to increment x or decrement n so as to leave
the loop if the destination field is used up before a NUL is found.

Can I play? I like while loops:

char *strncpy(char *d,char *s,int n)
{       char *x = d;
        while (n-- && *s) *d++ = *s++;
        if (n) *d = '\0';
        return x;

Quote:
}

Morris Dovey

Des Moines, Iowa, USA


Wed, 02 Aug 2000 03:00:00 GMT  
 another newbie question: strncpy function


Quote:
>Can I play? I like while loops:

>char *strncpy(char *d,char *s,int n)
>{      char *x = d;
>       while (n-- && *s) *d++ = *s++;
>       if (n) *d = '\0';
>       return x;
>}

This function does not satisfy the definition of strncpy().  Even if you
changed the parameter list to match what the Standard says it should be:

char *strncpy(char *d,const char *s,size_t n)

The result of the function still fails to comply.

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>



Wed, 02 Aug 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. strncpy/strncpy

2. Newbie Array Problem - how to strncpy from an array of char

3. Newbie Array Problem - how to strncpy fr

4. STRNCPY FUNCTION??

5. strncpy question

6. A question with strncpy, help please!

7. A question with strncpy, help please

8. strncpy question?

9. Question about strncpy() and memcpy()!

10. newbie question: returning structs from functions

11. Newbie Function question

12. Newbie question about function to get float

 

 
Powered by phpBB® Forum Software