Is it OK that a char* variable will point to a string (that’s written in the source code)?
Can I manipulate/modify the space allocated for the literal string as long as I’m not exceeding it’s length?
As much as I understand the format of an executable, it’s fine, but I want to be sure. Thank you 🙂
Is it OK that a char* variable will point to a string (that’s written
Share
Depends a bit on your runtime characteristics, but in general, doing something like:
will compile, but not work at runtime. The literal string in this case is generally in a read-only section of the executable. The following example, however, will work:
In this case, the literal string is an initialiser for an array (
s) on the stack. So the answer to your first question is “yes” and the answer to your second question is “maybe”.There is a semantic difference between the two examples. The first one creates a pointer to a literal string, and the second creates an array and initialises it with the contents of a literal string.