I’m having a rather strange issue on a QNX6.3 SP2 x86 target. The following code suspends the thread with a SIGSEGV when the ‘c[1]’ code line is executed. If
c is defined as ‘char c[] = “$AAM\r\0”;’ the thread is not suspended.
#include <stdlib.h>
int main (void) {
char *c = "$AAM\r\0";
c[1] = '0'; // SIGSEGV if char *c
c[2] = '1';
return EXIT_SUCCESS;
}
The issue persists with qcc version 2.95.3 as well as 3.3.5.
A string literal can be used in two slightly different ways. As an array initializer (as in the declaration of char c[]), it specifies the initial values of the characters in that array. Anywhere else, it turns into an unnamed, static array of characters, which may be stored in read-only memory, which is why you can’t safely modify it. In an expression context, the array is converted at once to a pointer, as usual so the second declaration initializes c to point to the unnamed array’s first element.