SHORT:
This is prototype of printf
function in C:
int printf ( const char * format, ... );
You should pass c-string (like "this is my message") instead of char
.
DETAILED:
This is prototype of printf
function in C:
int printf ( const char * format, ... );
This means that the first argument should be a pointer to a null-terminated array of char
. In fact, printf
reads value of first argument which is address of an c-string in memory, then go to that address and reads bytes by bytes to reach null character. In two condition this code causes segmentation fault:
- The address is pointed by the first argument of
printf
is outbound of memory address of your program.
printf
can't find any null characters beginning from the specified address before reaching end of memory boundary of your program.
Please be careful about using non-pointer variables in place of pointers. This cause your program to crash without a argumentive reason.