PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Re: Bug/Gross InEfficiency in HeathField's fgetline program
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: Bug/Gross InEfficiency in HeathField's fgetline program

Réponse
 
LinkBack Outils de la discussion
Vieux 20/10/2007, 13h39   #1
Charlie Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.


  Réponse avec citation
Vieux 20/10/2007, 18h00   #2
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Bug/Gross InEfficiency in HeathField's fgetline program

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h43.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,07580 seconds with 10 queries