|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
"Richard Heathfield" <rjh@see.sig.invalid> a écrit dans le message de news:
qdydnRU5I5AsFZHanZ2dnUVZ8qWhnZ2d@bt.com... > Tor Rustad said: > > <snip> > >> I ended up recommending the only one, with 0 work experience, who >> admitted he didn't knew C well. The seniors, failed big time >> implementing strncpy() on the blackboard. Very embarrassing. > > > Well, I'm game. Is this a blackboard? Why, yes, it is (although it's > actually white, but never mind). > > Okay, it's an interview, so I'm not allowed to look stuff up. So, off the > top of my head, strncpy copies no more than n characters from s to t, > stopping at a null terminator if present, and zero-padding t. It then > returns t. I can't actually remember whether n is size_t or int. (It ought > to be size_t, of course, but then so ought the n in fgets.) So I'll risk > embarrassment by plumping for size_t. > > #include <stddef.h> > > char *strncpy(char *t, const char *s, size_t n) > { > char *u = t; > while(n > 0 && *s != '\0') > { > *t++ = *s++; > --n; > } > while(n-- > 0) > { > *t++ = '\0'; > } > return u; > } > > How did I do? Should I start blushing yet? Why do you include <stddef.h> instead of <string.h> ? -- Chqrlie. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Charlie Gordon said:
> "Richard Heathfield" <rjh@see.sig.invalid> a écrit dans le message de > news: qdydnRU5I5AsFZHanZ2dnUVZ8qWhnZ2d@bt.com... >> <strncpy implementation> >> >> How did I do? Should I start blushing yet? > > Why do you include <stddef.h> instead of <string.h> ? I needed a definition for size_t. This is defined in <stddef.h> (as well as in other places), which is why I included it. I didn't see any particular need to include <string.h>, since it contained nothing I remembered needing. On reflection, it would have been useful to pick up the <string.h> prototype of strncpy, just on the off-chance that I misremembered the n type. (And yes, had I done so, I could have omitted the <stddef.h> header completely.) -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999 |
|
![]() |
| Outils de la discussion | |
|
|