c - why segmentation fault in string but not char? -


i want print first character of string, string(%s format specifier) , leads segmentation fault. when using %c format specifier works fine. why happening ?

#include<stdio.h> #include<string.h> int main() {     char *str = "feasible" ;     printf("%s",*str);     return 0; } 

is there perfect reason segmentation fault occur ? , segmentation fault in code ?

the segmentation fault occurs because string char *, pointer char. when pass *str in

printf ("%s\n", *str); 

you pass char value str points (the first char in string) printf. printf expects pointer first char of string , tries acces given memory location, results in segmentation fault.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -