-2

Are those two sentences correct regarding a microcontroller?
1. const char* longString1 = "Foo1";
In this case the string will be stored in the data area in the memory.
2. char* longString2 = "Foo2";
In this case the string is placed in the text\code area in the memory.

Where and which is the best way to store strings if the platform are the following microcontrollers:
1. PIC?
2. ARM (to be more specific, ARM Cortex-M4F Based MCU TM4C123G)?

Ronen333
  • 69
  • 1
  • 8
  • C does not specify either `data` or `text\code`, and writing to a string literal is undefined behaviour. You'll need to mention your platform, compiler and compilation flags if you want an answer. – Quentin Feb 12 '18 at 10:16

1 Answers1

0

The best way is the const char* that conveys the idea that the pointer can point to other thing but the thing it points to is constant (string literals are expected not to be modified by the standard. Standard says it is undefined behavior in trying to do so.)- cant be modified. Second will generate warning. This is a risk that you will leave open.

Also the main thing is - those are commonly kept in read only section but that might vary given that standard doesn't put any constraint other than the fact modifying them is undefined behavior. How that will be realized is a big choice left to implementors. One can simply do this also - that memory on which literal is kept is not read only but modifying them invokes behavior dependent on that platform.

Also are these statements true? is a question which has lots of things to be specified. The platform on which you are running and what architecture is there.

Literal strings are not const char[] arrays, but only char[] arrays that are forbidden to be overwritten. You can realize the behavior without storing them in read only memory too. But depending on the declaration segregating the memory location where it is stored is convoluted design - rather it's better to keep it in same memory irrespective of declaration for the same string literal(that is the case in most cases).

user2736738
  • 30,591
  • 5
  • 42
  • 56
  • The question is about which one to use!! and that is explained and why is that. – user2736738 Feb 12 '18 at 10:18
  • 2
    OP is apparently on a platform which stores string literals in different places depending on their declarations. The questions are whether this is true, and what is the "best" storage location. This answer answers none. – Quentin Feb 12 '18 at 10:22