Help needed: SIGSEGV with char *c' but not with 'char c[]'

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.

Any one has a glue?


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.

If you set the warning to maximum level, you would have obtained:

warning: initialization discard qualifiers from pointer target type.

To get rid of the warning you need the const qualifier.

const char *c = “$…”;

Now declared like this the c[1] would have resulted in a compiler error.

So what have we learned today; always use maximum level of warning ;)

Thank you all for the feedback.

I assume max warn level is -w9, right?
I tried to recompile with -w1 as well as -w9; the compiler throws no warning in either case.

Does for me
qcc -w9 file.c