c++ - Understanding the Warning and Compilation Error while Initializing and Declaring Pointer Variable -
this question has answer here:
please @ following code snippet -
int main(){ int *i_ptr = 5; printf("%d", *i_ptr); return 0; }
here trying declare , initialize pointer variable i_ptr
. gives me following warning while compiles fine -
warning: initialization makes pointer integer without cast [enabled default]
but when going execute code gives me following error -
segmentation fault (core dumped)
i know correct way -
int n = 5; int *ptr = &n;
now have questions -
1. while first code fails @ execution time why doesn't give compilation error, instead of warning?
2. can initialize , declare pointer variable -
int n = 5 // both declaration , initialization of int type variable n
thanks in advance.
- while first code fails @ execution time why doesn't give compilation error, instead of warning?
the difference between errors , warnings depends on flags pass compiler.
it's idea make setting more paranoid during development (-wall
, -wextra
, possibly -weverything
clang), in addition making warnings errors via -werror
(which shouldn't set default when shipping code).
- can initialize , declare pointer variable
pointer variables store addresses, , need declare addressable can store value 5.
since c99, it's possible use compound literal instead of named variable:
int *p = &(int){ 5 };
Comments
Post a Comment