1
The following method I found on the internet, serves to create a string copy.:
char* bin_copy_string(const char* begin, const char* end) {
char* result;
result = malloc(end - begin);
if (result) { // result is a valid pointer, and not a null pointer
char* p = result;
while (begin != end) {
*(p++) = *(begin++);
}
}
return result;
}
My question is on the line char* p = result;
, why the new pointer p
is modifying the string result
but in the end returns result
? You couldn’t just do *(result++) = *(begin++);
?
strdup()
is not part of the standard library C is a peculiarity POSIX. It would be better to use the AP example withstrcpy()
.– Augusto Vasques