john@basho.uucp (John Lacey) (09/02/90)
Several people have been having problems using strcpy on various machines. Those problems have nothing to do with the machine, operating system, or compiler. The submitted pieces of code look something like this: char * dest; char * src = "foobar"; strcpy ( dest, src ); printf ( "Contents of dest: %s\n", dest ); The problem is that no storage has been allocated for dest. One person claims that changing the definition of dest to char dest[]; works. Well, it shouldn't. C requires that the defining declaration of an array have an explicit size given. But something like this will work. We need storage, so we can allocate it statically, like this: char dest [100]; The disadvantage of this is that the size of dest will be fixed at compile time. (Also, using a macro in place of the fixed number is probably better.) Am improvement is to allocate the storage at run-time, depending on the size of the source string. Also, in true ANSI style, we should be explicit about whether our source string is writable or not (char src[] = "foobar", or const char * src = "foobar"). This leads us to something like the following: char * dest; const char * src = "foobar"; dest = (char *) malloc ( strlen ( src ) + 1 ); strcpy ( dest, src ); printf ( "Contents of dest: %s\n" ); Sorry for such a pedantic posting. Toodles and cheers, John -- John Lacey, E-mail: ...!osu-cis!n8emr!uncle!basho!john (coming soon: john@basho.uucp) Voice: (614) 436--3773, or 487--8570 "What was the name of the dog on Rin-tin-tin?" --Mickey Rivers, ex-Yankee CF