It looks like you are experimenting with inputPtr[0]
as a substitution for *inputPtr
. In many contexts, the two expressions produce the same result.
However, expression inputPtr[0]++
is not the same as *inputPtr++
, because [0]
has higher precedence than *
, but it has the same precedence as suffix ++
. Operators within this precedence level are applied left-to-right, so the first expression post-increments inputPtr[0]
, a character inside a character literal. This is undefined behavior, hence you see a crash.
If you replace inputPtr[0]++
with *inputPtr++
and remove inputPtr++
from loop header, your code is going to work fine:
for (char* inputPtr = cc; inputPtr[0]; ) {
char c = *inputPtr++;
printf("%c", c); // Replace %s with %c to print one character
}