String Literals

Are string literals put on the stack? Dose that mean that they automatically - uh - disappear like regular ints and stuff? Is this safe

1
2
3
4
void fnc(){
  char* x = (char*)"bijhjbhjbjhbjhvuhv";
  x = (char*)"jjjki";
}


and this illegal?

1
2
3
4
char* fnc(){
  char* x = (char*)"gghh";
  return x;
}


Am I asking stupid questions?
Edit: actually if I was asking stupid questions I’d know the answer so these are fine lol I’m dumb.
Last edited on
Are string literals put on the stack?
No. They're put in a data segment that can be read-only where possible. That's why you had those warnings that you've attempted to cast away.

The corrected version are:
1
2
3
4
5
6
7
8
9
void fnc() {
  const char* x = "bijhjbhjbjhbjhvuhv";
  x = "jjjki";
}

const char* fnc() {
  const char* x = "gghh";
  return x;
}

And yes, they're legal.
Last edited on
that you've attempted to cast away.

XD ya know that about sums up the entirety of my programming experience right there lol.
Thank you, I forgot about that. :)
Topic archived. No new replies allowed.