Char* to Char array

So it is pretty self explanatory but I am trying to put a char* into a char

I tried
char hold[100];
char *word=new char[size];

but am having problems copying a char* into a char, how would I go about doing something like that? Thanks
What are you coping when the memory pointed by word contains some garbage?!
Last edited on
hold is an array of char, but it is unitialized. The contents are up for grabs.

word is a pointer pointing to an array of chars allocated on the heap, also unitiailzed.

The first command, strcpy(hold, word); should be the one you want. The others should be syntax errors. However, because word is unintialized, there are things you need to consider.

strcpy() copies until it reaches a null character '\n', and then stops copying. If the first character of word is the null character, only 1 char is copied.

strcpy will not stop copying until it reaches a null character. If all of word is non-null characters, strcpy() will very silently continue copying from beyond the limits of word, and more importantly into beyond the limits of hold. This is dangerous. To protect against buffer overruns, use strncpy instead.
Null character is '\0'.
'\n' is the newline character.
Topic archived. No new replies allowed.