I wrote this code
#include <stdio.h>
int main()
{
char String[] = "Hello the world!";
char *pointer = String;
int i;
printf(" %s\n", pointer);
pointer = "Helllooooo the worlldddddd";
printf("Hello %s\n", pointer);
printf("Hello %s\n", String);
return 0;
}
but I can't understand how this line works fine.
pointer = "Helllooooo the worlldddddd";
but I got this output
Hello the world!
Hello Helllooooo the worlldddddd
Hello Hello the world!
As you see it couldn't change String value but it shows more than the original number of characters. Shouldn't this cause a buffer overflow? Won't that destroy other variables?
"Hello the world!"and"Helllooooo the worlldddddd"are two different strings, located in two different parts of memory. Withpointer = ..., you are settingpointerto be the address of either one of these strings (i.e., setting it to point to that string).