Please look at the following code snippet -
int main(){
int *i_ptr = 5;
printf("%d", *i_ptr);
return 0;
}
Here I am trying to declare and initialize a pointer variable i_ptr
. It gives me the following warning while it compiles fine -
warning: initialization makes pointer from integer without a cast [enabled by default]
But when I am going to execute the code it gives me the following error -
Segmentation fault (core dumped)
I know a correct way to do this -
int n = 5;
int *ptr = &n;
Now I have some questions -
1. While the first code fails at execution time why it doesn't give compilation error, instead of warning?
2. Can we initialize and declare pointer variable like this -
int n = 5 // both declaration and initialization of int type variable n
Thanks in advance.