-3

Im doing something wrong.

This is the piece of code from ARM code (Keil5 IDE):

uint8_t * at_ss_data = (uint8_t *)("\n\rAT$SS=AA AA\n\r");
at_ss_data[12] = 0;

but the 12th index (the last A) does not change in the variable when the code is pushed to the ARM embedded board.

My goal is to change the AA AA substring in the at_ss_data array to 00 00

mnille
  • 1,328
  • 4
  • 16
  • 20
MarcelH
  • 43
  • 5
  • 9
    Modifying a string literal is undefined behavior. – EOF Jul 27 '16 at 11:37
  • See http://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s-in-c – Garf365 Jul 27 '16 at 11:44
  • @Notlikethat: In C, string literals are **not** of type `char const *`. You're thinking of c++. – EOF Jul 27 '16 at 11:44
  • Or another one : [Modifying C string constants?](http://stackoverflow.com/questions/480555/modifying-c-string-constants) (it's last, I stop to search other same question) – Garf365 Jul 27 '16 at 11:48
  • @Garf365: One unfortunate problem with those Q&A's is that the answers focus on the observed behavior, rather than clarifying that the behavior is *undefined*. In particular, in this case there is no segfault. – EOF Jul 27 '16 at 11:48
  • @EOF it's on embedded device, string is certainly stored in flash, so read-only – Garf365 Jul 27 '16 at 11:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/118427/discussion-between-eof-and-garf365). – EOF Jul 27 '16 at 11:52
  • @EOF: Formally correct. But they are nevertheless immutable and in typical implementations technically identical with `const char []`. They are never `const char * ` nor `char *` which are pointers. Neither in C nor C++. – too honest for this site Jul 27 '16 at 11:59
  • @Olaf If they *were* of type (or "technically identical to", whatever that would mean to you) `char const []`, you could not convert them to `char *` by assignment. – EOF Jul 27 '16 at 12:05
  • @EOF: That's why I wrote "technically". gcc e.g. puts them into `.rodata` section - the same it puts `const char []` into. The assignment to a `char *` is some legacy not to break "problematic" code. That this works with a pointer is the standard automatic conversion of an array to a "pointer to the first element". – too honest for this site Jul 27 '16 at 12:11
  • 2
    @Olaf: Nothing "technically" about it. String literals *do not have* type `char const[]`. If they *did*, the compiler could catch this kind of problem, *but they do not*. This is important. Teach people to understand C's type system, or they'll be perpetually confused. – EOF Jul 27 '16 at 12:14
  • @EOF: Types are semantical, not technical. Where exactly did I write a string literal has `const char []`? They are not `const char *` nor `char *` either, but `char []` in C and `const char []` in C++! Anyway, either I cannot make clear what I mean or you don't want to understand. Either way, this is useless. – too honest for this site Jul 27 '16 at 12:16

2 Answers2

3

You must not modify a string literal (which is undefined behavior). You should use an array initialized with a string literal.

This way, your code should be:

uint8_t at_ss_data[] = "\n\rAT$SS=AA AA\n\r";
at_ss_data[12] = 0;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
blatinox
  • 833
  • 6
  • 18
2

at_ss_data is pointing to read-only memory, you can't change string literal like this. This behavior is undefined. instead you may try something like this:

uint8_t at_ss_data[] = "\n\rAT$SS=AA AA\n\r";

This array is now writable.

rj99999
  • 109
  • 10
  • 2
    _you are allocating memory on the stack_.... It depends on the scope of the array. – LPs Jul 27 '16 at 11:59
  • FWIW, it is undefined behaviour, probably made so because string literals *often* reside in read-only memory, so you should treat them as such. But they do not always reside in read-only memory. – Rudy Velthuis Jul 27 '16 at 18:25