MESSAGE
DATE | 2005-01-25 |
FROM | From: "Martin, Jared"
|
SUBJECT | RE: [hangout] Simple C question
|
Thanks a lot, I had seen strcpy, but strncpy is what I really needed Thanks.
Sincerely, ***************************** Jared Martin IT Desktop Support Davis & Gilbert LLP 1740 Broadway New York, NY 10019 Help Desk: 212-237-1500 Office: 212-237-1504 Cell: 646-773-4138 Fax: (212) 974-7023 *************************************
-----Original Message----- From: Billy [mailto:billy-at-dadadada.net] Sent: Tuesday, January 25, 2005 4:03 PM To: Inker, Evan Cc: Martin, Jared; NYLXS Subject: Re: [hangout] Simple C question
Inker, Evan wrote: > Check this out > > strcopy.c > > /* strcopy.c written by detour-at-metalshell.com > * > * example of a string copying function. > * > * http://www.metalshell.com/ > * > */ > > #include > #include > > void strcopy(const char *, char *); > > int main() { > char string1[] = "This is string1"; > char string2[] = "This is string2"; > > strcopy(string1, string2); > printf("String2: %s", string2); > } > > void strcopy(const char * str1, char * str2) { > int x = 0; > > /* change the size of string2 to the size of string1 */ > realloc(str2, sizeof(str1)); > > do { > str2[x] = str1[x]; > } while (str1[x++] != '\0'); > > }
This program is incorrect. YOU CANNOT overwrite the contents of string2. It is also illegal to realloc() a pointer which was not obtained by malloc(). also, realloc is free to move the block. you have to use its return value to update the pointer. There's just so much wrong...
I DON'T RECOMMEND learning from this example.
____________________________ NYLXS: New Yorker Free Software Users Scene Fair Use - because it's either fair use or useless.... NYLXS is a trademark of NYLXS, Inc
|
|