c - Can a string literal and a non-string non-compound literal be modified? -


string literals lvalues, leaves door open modify string literals.

from c in nutshell:

in c source code, a literal token denotes a fixed value, may integer, floating-point number, character, or string. literal’s type determined value , notation. literals discussed here different compound literals, introduced in c99 standard. compound literals ordinary modifiable objects, sim‐ ilar variables.

although c not strictly prohibit modifying string literals, should not attempt so. 1 thing, the compiler, treating string literal constant, may place in read-only memory, in case attempted write operation causes fault. another, if 2 or more identical string literals used in program, compiler may store them @ same location, modifying 1 causes unexpected results when access another.

  1. the first paragraph says "a literal in c denotes fixed value".

    • does mean literal (except compound literals) shouldn't modified?

    • since string literal isn't compound literal, should string literal modified?

  2. the second paragraph says "c not strictly prohibit modifying string literals" while compilers do. should string literal modified?
  3. do 2 paragraphs contradict each other? how shall understand them?

  4. can literal neither compound literal nor string literal modified?

thanks.

from c standard (6.4.5 string literals)

7 unspecified whether these arrays distinct provided elements have appropriate values. if program attempts modify such array, behavior undefined.

as statement.

the second paragraph says "c not strictly prohibit modifying string literals" while compilers do. should string literal modified?

then compilers not modify string literals. may store identical string literals 1 array.

as @o11c pointed out in comment in annex j (informative) portability issues there written

j.5 common extensions

1 following extensions used in many systems, not portable implementations. inclusion of extension may cause strictly conforming program become invalid renders implementation nonconforming. examples of such extensions new keywords, library functions declared in standard headers, or predefined macros names not begin underscore.

j.5.5 writable string literals

1 string literals modifiable (in case, identical string literals should denote distinct objects) (6.4.5).


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -