On Wed, 2009-03-18 at 22:05 -0600, Grant Robinson wrote: > char *myCString = new char[50]; > strcpy(myCString, "My C string");
Friends don't let friends use strcpy(). One possible option is strlcpy(): http://en.wikipedia.org/wiki/Strlcpy A more portable option is strncpy(): #include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { const char *foo = "short string"; const char *bar = "much much longer string"; char buffer[15]; printf("sizeof(buffer) = %d\n", sizeof(buffer)); strncpy(buffer, foo, sizeof(buffer)); buffer[sizeof(buffer)-1] = 0; printf("source = '%s'\n", foo); printf("buffer = '%s'\n", buffer); strncpy(buffer, bar, sizeof(buffer)); buffer[sizeof(buffer)-1] = 0; printf("source = '%s'\n", bar); printf("buffer = '%s'\n", buffer); } -- "XML is like violence: if it doesn't solve your problem, you aren't using enough of it." - Chris Maden -------------------- BYU Unix Users Group http://uug.byu.edu/ The opinions expressed in this message are the responsibility of their author. They are not endorsed by BYU, the BYU CS Department or BYU-UUG. ___________________________________________________________________ List Info (unsubscribe here): http://uug.byu.edu/mailman/listinfo/uug-list
