so I was playing with pointers because I didn't know what else to do and usually, I imagine what's going on under the hood after each instruction. But I recently came against an error that I don't really understand.
char *str = "test"; printf("%c", ++*str); Output :
zsh: bus error Expected output was a 'u' because as far as I know, it first dereference the first address of the variable 'str' wich is a 't' than increment it right ? Or am I missing something ?
Changing the code like so is not giving me any error but why ?
printf("%c", *++str); Thank you !
char *str = "test";is not correct in C++, so I assume this is C. Please only tag the language you are actually using++*strattempts to increment the read-only data in the string literal.printf("%c", ++*str);. Break out your increment into a separate statement. As @WilliamPursell explains, the dereference occurs first, before the increment.char buf[] = "test"; char *str = buf; ...char str_data[] = "test"; char *str = str_data;orchar *str = (char[]){ "test" };instead,