Hello guys,
Here is the test code which is segfaulted:
cat ptr.c
#include <stdio.h>
int main()
{
char *ch = "abc";
ch++;
*ch = "A";
The compiler should have produced a diagnostic message
for this line, because the things on the left and right of
the assignment have incompatible types:
- Since `ch' is a "pointer to char," `*ch' is a char.
Whatever you attempt to store in `*ch' must therefore
be convertible to a char.
- However, "A" is a string literal. In this context,
it denotes an array of two char elements, the first
being an 'A' and the second a '\0'. And in this
context (as in most), mentioning an array yields a
pointer to the array's first element. So, the thing
on the right is a value of type `char*'.
So: On the left, a reference to someplace that can store
values of type `char'. On the right, a value of type `char*',
which is not at all the same thing. The compiler is required
to complain. After complaining, it can reject the program or
"make corrections and carry on" -- but what corrections it might
have made, and how it carries on, are not specified by C. If
the program runs, it might do anything at all.