1

I noticed that when trying to modify a string declared in the first way the result is a segmentation fault at runtime.

Why is that? What was the logic behind this behavior? Is this standard C behavior?

  • 1
    It's nearly an exact duplicate but it might be helpful to note a subtle differnce between intializers and expressions. The initializer of an array gives the initial contents of the array, which is writable. The initializer for the pointer erodes to a pointer with the neither-const-nor-writable oddity described above. – NickJH Sep 19 '17 at 08:18
  • @NickHollinghurst Thank you for pointing this out. It seems a little confusing for me now but I think as I learn more I will understand this better. However can you tell me what does "erode" means in this context? –  Sep 19 '17 at 08:36
  • Whoops, I used the wrong word! I should have said "decay". More formally "array-to-pointer conversion": It happens when an array is passed to a function or used in any expression that can take a pointer. But the interesting case is the second one, because there the string literal doesn't exist as a separate object, only as an array initializer. – NickJH Sep 19 '17 at 09:26

0 Answers0