MESSAGE
DATE | 2005-01-26 |
FROM | Ruben Safir Secretary NYLXS
|
SUBJECT | Re: [hangout] Simple C question
|
Document pelase because it works just fine
#include #include
char a[]="Hello Earth"; char b[]="Hello Moon";
int main(int argc, char ** argv){
strncpy(a,b,10); printf("%s\n",a); exit; }
ruben-at-www2:~> ./a.out Hello Moonh
On Wed, Jan 26, 2005 at 10:52:06PM -0500, Billy wrote: > > Ruben Safir wrote: > >On Tue, 2005-01-25 at 16:03, Billy wrote: > > > >>Inker, Evan wrote: > >>> > >>>#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. > > > > > > > >Why not? It is not a string literal. > > char string2[] = "This is string2"; > > Yes it is. > > > > >>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. > >> > > > > > >I agree. There is no reason to rewrite strncpy > > > ____________________________ > NYLXS: New Yorker Free Software Users Scene > Fair Use - > because it's either fair use or useless.... > NYLXS is a trademark of NYLXS, Inc
-- __________________________ Brooklyn Linux Solutions
So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998
DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002 http://fairuse.nylxs.com
http://www.mrbrklyn.com - Consulting http://www.inns.net <-- Happy Clients http://www.nylxs.com - Leadership Development in Free Software http://www2.mrbrklyn.com/resources - Unpublished Archive or stories and articles from around the net http://www2.mrbrklyn.com/downtown.html - See the New Downtown Brooklyn....
____________________________ NYLXS: New Yorker Free Software Users Scene Fair Use - because it's either fair use or useless.... NYLXS is a trademark of NYLXS, Inc
|
|