[09:48:59] < gnuvince> jamessan, jfincher: remember the char *s = "hello"; thing where you can't modify s
without getting a segfault because it's in a non-mutable memory space or something like
that? Is that an optimisation that compilers do?
[09:52:08] < jamessan> no, it's how your defining things. 'char *s = "hello";' creates a pointer s that has the
initial value of the address of the string literal "hello". string literals are stored in
a non-mutable section of the programs address space
[09:52:48] < jamessan> 'char s[] = "hello";' creates an array of characters with the initial values 'h', 'e',
'l', 'l', 'o', '\0'.
[09:54:12] < jfincher> gnuvince: it's a super-standard thing, afaict
[09:55:01] < jamessan> the string literal itself is still stored in a non-mutable section of the address space,
but things act differently because you're defining variables of two different types
[09:55:22] < jfincher> gnuvince: when you get around to learning assembly language, you'll see that programs
have segments, and some of those segments are writable, and some aren't. When you say
'char *s = "hello";' the C compiler puts the literal in a non-writable segment.
[09:55:27] < jamessan> they have similarities in how you can access them, but that's just because of how arrays
are implemented
[09:56:11] < gnuvince> so if I do printf("Hello world\n");, the string in that call is stored in a non-mutable
section of the address space?
[09:56:20] < jfincher> yes
[09:57:33] < gnuvince> jamessan: when you say "things act differently", what do you mean? if char s[] = "hello";
stores the string in non-mutable memory, what happens when you do s[0] = 'j';? It
changes places before the assigment?
[09:58:13] < jfincher> gnuvince: char s[] = "hello" will store it in a writable segment
[09:58:20] < jamessan> gnuvince: the string literal "hello" itself is in the non-mutable section, but the
assignment to s creates a copy of the string in a writable section
[09:58:51] < gnuvince> Does this mean that char s[] = "hello" creates two instances of "hello"?
[09:59:03] < jamessan> that's probably optimized away so there isn't anything in the non-mutable address space
[09:59:57] < gnuvince> OK.
[10:00:05] < gnuvince> I'll need to check my reference book tonight
[10:00:07] < gnuvince> Thanks