Business_Woman said:
I want the function to return a pointer to the copied string
PHP:
char *strcopy(const char *src, char *dest){
while(*dest++ = *src++);
return dest;
}
Because you are incrementing your dest pointer, while copying the string, in the end it will point to the memory location right after the end of the destination string. Using that
pointer surely gives a segfault. You would need to save a copy
of the original dest pointer and return that instead.
Also what is the difference between
char *p;
*p = 'p'
This assigns the ASCII code of the letter 'p' to the memory location p points to.
This assigns a pointer to the memory location of the NUL
terminated string "p" to the memory location, where p points to.
As p is a "char *", this does not make much sense anyways. It
could make sense if p was of type "char **".
Do i still have to point p somewhere?, and where would that be?
p must point to a memory location, owned by and writable by the process. Otherwise a segfault is for sure. This could for example be the memory of an automatic variable, as in
Code:
char str[256];
char *p = &str;
or a memory block, allocated by means of malloc(), calloc(), etc.