As others have said, that should not be done that way, since you are modifying something that is supposed to be (and often is) unmodifiable.
char *p = "Sanfoundry C-Test";
This declares a pointer and points it to (sets the address contained in the pointer to the start of) the literal text (which is constant and should not be modified and probably can't be modified without an error anyway) "Sanfoundry C-Test"
.
But AFAIK, you are asking what the rest of the code means, so let's first correct the problem:
char p[] = "Sanfoundry C-Test";
That declares an array of char
with the given contents (the characters 'S'
, 'a'
, 'n'
, etc., followed by a 0 character). Such an array is treated as a text string, by C. Now
p[0] = 'a';
Changes the first character of that array (arrays "start counting" at 0), so the 'S'
in the string is changed to an 'a'
.
p[1] = 'b';
This changes the second character into 'b'
. So now the string is "abnfoundry C-Test"
. The final printf()
then displays that value in a console.