What happens behind the scene when a string in double quotes is passed as an argument to a function? Where is memory allocated for that string?
If I do memcpy(ptr, "SINGH", strlen("SINGH")+1);
, output is SINGH
only. From where is the extra byte copied from?
For below program:
void func(char *ptr) {
memcpy(ptr, "SINGH", strlen("SINGH"));
cout << ptr << endl; /* Output is SINGHHELLO */
/* If I do memcpy(ptr, "SINGH", strlen("SINGH")+1); Output is SINGH only. From where does extra byte is copied from? */
}
int main() {
char str[11] = "HELLOHELLO";
char str2[10] = "ABC";
memcpy(str, "HELLO", strlen("HELLO")); /* Where is memory allocated for string 'HELLO' ? */
cout << str << endl << str2 << endl;
func(str);
return 0;
}