#include<stdio.h>
int main(void)
{
char * p= "strings are good";
printf("%s",*p);
return 0;
}
Could somebody please tell me why I am getting segmentation fault here?
Could somebody please tell me why I am getting segmentation fault here?
If a conversion specification is invalid, the behavior is undefined.282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
1 undefined behavior behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements.
2 NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).
*p is of type char. %s expects argument of type char *. This will invoke undefined behavior. In this case anything could happen. Sometimes you may get the expected result and sometimes not. This may also cause program crash or segmentation fault (which is the case here).
For %s, you need to pass the starting address of the sting/literal.
Change
printf("%s",*p);
to
printf("%s",p);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With