20/10/2007, 13h39
|
#1
|
|
|
Re: Bug/Gross InEfficiency in HeathField's fgetline program
"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.
|
|
|
|