I have a noob question here, cannot understand whats wrong here. I have the following code here:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void copyFrom(char * mStr, char * str);
int main() {
char * srcStr = "Robert bought a good ford car";
char * dstStr = NULL;
copyFrom(srcStr, dstStr);
printf("The string copied here is %s", dstStr);
return 0;
}
void copyFrom(char * str, char * mStr)
{
if(!mStr) {
mStr = (char *)malloc((strlen(str)) + 1);
if(mStr == NULL)
return;
}
while(*mStr++ = *str++) {
;
}
mStr[strlen(str)] = '\0';
}
This does not copy the string, But if array is used instead of char pointer for dstStr, it all works fine.
Can you please tell me what is wrong here?