No for a few reasons:
temp
isn't initialised, it's pointing to some random location and doesn't have any memory allocated for it, so you're just going to be writing over a random part of memory (and/or crash).
a
is a single char
and you're treating it's value as a string (char*
) using strcpy
(I'm assuming you meant (char*)&a
which is still wrong).
strcpy
continues copying char
s from the source (a
) to the destination until it hits a '\0'
in the source...which could be anywhere since a
is not a NUL terminated string but a char
.
If you want to write/append a single char
to a string/buffer you just do buffer[position] = character
, where buffer
is a char[]
or a char*
pointing to a block of allocated memory, position
is the position in the buffer you want to stick the char
, and character
is obviously that char
.
Anyway, I've got no idea what you're trying to do or the logic behind why you're trying to do whatever it is you're trying to do like this.
EDIT: And also you have this tagged as C++, why aren't you using std::string
and std::cin
if you are in fact using C++?