I have a program as shown below : This one works fine why? isn't it supposed to be constant for each of the characters? why am I able to overwrite it?
input: "Test" (Any String that is shorter dan 8 last byte reserved for null-terminated)
#include <iostream>
using namespace std;
int main()
{
const char itemss[8] = "1234567";
const char* item = items;
scanf_s("%s", item, 8);
printf(item);
return 0;
}
but when I try to change the code to this :
this one doesn't work, the code is the same as the above code but this one cause segmentation fault, string literal is const char[] (in this case const char[8]) so it should be no different than the code above right? my guess is this code below allocated in read-only memory but then again the code above also use const.
#include <iostream>
using namespace std;
int main()
{
const char* item = "1234567";
scanf_s("%s", item, 8);
printf(item);
return 0;
}