0

is

char* array[] = {"first","second","other one","last"};

an array of char* ?

the strings (array[0],...) can not be modified even with a cast to char*. where is the string putten on memory ?

can not be modified = "array[0][0] = 'a'"; does not work.

Hicham
  • 983
  • 6
  • 17
  • 1
    "where is the string putten on memory" is just wrong. A cast doesn't touch the memory, it just tells the compiler to interpret a variable in a certain way. – ThiefMaster Nov 09 '11 at 07:45
  • Possible duplicate of: http://stackoverflow.com/questions/349025/is-a-string-literal-in-c-created-in-static-memory – themel Nov 09 '11 at 07:47
  • ThiefMaster : you misunderstood. i said where in memory refering to in what memory : not stack, is it data memory, somewhere special on the heap ? it could help me to understand the work of the compiler. – Hicham Nov 09 '11 at 13:19

2 Answers2

2

It is an array of char* but you have initialized it with string literals. These are read-only and attempting to modify them is undefined behaviour.

It is common for a compiler to store literals in a read-only segment of the executable image which is probably why you are seeing segmentation faults when you attempt to modify the contents.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

Your strings are of type const char * and can be stored in ROM. Pointers to those locations are stored in the array.

Johan Bezem
  • 2,582
  • 1
  • 20
  • 47
  • is that related to BIOS? One is RAM and other one is ROM. – user103214 Nov 09 '11 at 08:08
  • 2
    @user974191: I do not quite understand your question. On a PC and similar systems, everything is stored in RAM (for normal programs). But for embedded systems, the compiler can use ROM-segments to store the code. Programming a BIOS is similar to creating an embedded system, and also identifies ROM and RAM areas. When using modern PCs, the code sections, even when stored in RAM, can be protected by the OS in combination with the processor to protect the memory segment and effectively create a read-only segment for the program code and/or read-only data. – Johan Bezem Nov 09 '11 at 08:18