I tried the following program:
#include <stdio.h>
char s1[] = "Global String";
char *s2 = "Another Global String";
main()
{
char s3[] = "Local String" ;
char *s4 = "Another Local String" ;
strcpy( s1, "New str" );
strcpy( s2, "New str" ); // causes seg fault
strcpy( s3, "New str" );
strcpy( s4, "New str" ); // causes seg fault
}
s2 and s4 cause segmentation fault, presumably because they are stored in read-only data segment. How come the literal strings pointed by s1 and s3 don't crash? This is on Ubuntu.
Strangely, s1, s2, s3 and s4 can all be modified and no crash occurs when compiled with gcc on cygwin. Why is this?