main()
{
char *p1;
char *p2 = "Now is the time";
strcpy(p1, p2);
}
This is bad. Think about what a pointer is - it is a variable that holds a memory address. In the case of p1, above, this is a character pointer which is two things. It is a pointer to NULL, or possibly garbage. It is also a pointer to
one single char. I assume you haven't learnt about the malloc function yet, so I suggest rewriting the declaration *p1 as p1[100] ( I'll also assume you have done arrays, as you are now on pointers ).
strcpy() works like so:
Step one, send pointer to char (our target pointer, p1) and another pointer to char (our input pointer, p2). Take the value at the address of (in our case) p2 and assign it to the value at the address of p1. Stop here for a second, what is p1 pointing to? ( look up ). It is either pointing to NULL or garbage. You need to have allocated memory for p1, before you can start storing things there.
In your second example, you are just overwriting memory that is ( possibly ) already in use. This is undefined and can go unnoticed, crash later, or crash immediately depending on your luck.
Well, that post was longer than expected, hopefully it made some sense.
