Article View: fr.comp.lang.c
Article #15486memcpy et char
From: Alain CROS
Date: Fri, 23 Sep 2005 11:39
Date: Fri, 23 Sep 2005 11:39
56 lines
2443 bytes
2443 bytes
Bonjour.
J'ai �crit une fonction qui remplace la virgule par un point dans une chaine de caract�res pour ensuite permettre de faire un
calcul.
Pour copier une chaine dans une autre chaine, j'utilise memcpy.
Est ce n�cessaire d'�crire
memcpy(ch, c, sizeof(char) * taille);
ou bien je peux me contenter d'�crire
memcpy(ch, c, taille);
Toutes critiques sur la fonction seront les bienvenues.
Merci.
Alain CROS
#include <string.h>
int convstrnum(int taille, char *c)
/* Remplace, dans une chaine de caract�res repr�sentant un chiffre,
la virgule par un point.
Retourne :
la position de ce point dans la chaine (base 1).
0 si pas de point.
-1 si pas un chiffre (la chaine reste inchang�e).
*/
{
int i = 1, j = 0, k = 0;
char ch[taille + 1];
char *pch = ch;
memcpy(ch, c, sizeof(char) * taille);
ch[taille] = '\0';
while(*pch != '\0')
{
if((*pch < '0') || (*pch > '9'))
switch(*pch)
{
case '.':
j = (++k == 2) ? -1 : j;
break;
case ',':
*pch-- = '.';
j = i--;
break;
default:
j = -1;
}
if(j == -1)
break;
*pch++;
i++;
}
if(j != -1)
memcpy(c, ch, sizeof(char) * taille);
return(j);
}
Technical Details
Message-ID:
<4333e995$0$17469$626a14ce@news.free.fr>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!newsread.com!news-xfer.newsread.com!newspeer.monmouth.com!newsfeed.icl.net!newsfeed.fjserv.net!oleane.net!oleane!in2p3.fr!univ-lyon1.fr!news.cs.univ-paris8.fr!proxad.net!infeed-2.proxad.net!news9-e.free.fr!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: f9822,366ad66ae342f8da,start X-Google-Attributes: gidf9822,public X-Google-Language: FRENCH,ASCII Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!newsread.com!news-xfer.newsread.com!newspeer.monmouth.com!newsfeed.icl.net!newsfeed.fjserv.net!oleane.net!oleane!in2p3.fr!univ-lyon1.fr!news.cs.univ-paris8.fr!proxad.net!infeed-2.proxad.net!news9-e.free.fr!not-for-mail From: "Alain CROS" <Personne@ICI> Newsgroups: fr.comp.lang.c Subject: memcpy et char Date: Fri, 23 Sep 2005 13:39:31 +0200 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 Lines: 58 Message-ID: <4333e995$0$17469$626a14ce@news.free.fr> Organization: Guest of ProXad - France NNTP-Posting-Date: 23 Sep 2005 13:40:05 MEST NNTP-Posting-Host: 62.147.10.124 X-Trace: 1127475605 news9-e.free.fr 17469 62.147.10.124:1054 X-Complaints-To: abuse@proxad.net