0
Why pointer variable without initialization prints 0,but not garbage?
we know that c language stores garbage value in a variable if it is not initialized, but in case of pointers,without assigning an address to pointer,printing pointer results 0 int x; print("%d",x); output:: some garbage value int *y: print("%d",*y); output:: 0
8 Antworten
+ 7
which compiler you use??
+ 6
Take an uninitialized pointer:
int* ptr;//points to any location in memory
but in turbo c++ shows 0
Take a null pointer:
int* ptr = NULL;//normally points to 0x0 (0)
Both would cause undefined behaviour if dereferenced. NULL is often defined as 0.
+ 6
i think it depends on compiler.. in turbo c++ shows zero both uninitialized and null pointer..
as http://rextester.com/l/c_online_compiler_gcc -gcc compiler also show 0
+ 2
a uninitialized pointer can point to garbage data, just yesterday i fixed a bug caused by a uninitialized pointer that was pointing to garbage. by the way: you shouldn‘t use turbo c++ it doesn‘t support c++11,c++14 and c++17 and has some nonstandart behavior
+ 1
you are saying an uninitialed pointer contains NULL value but because of turboc++ ,NULL value is showed as 0 !!!!
+ 1
it generated error ,
invalid memory reference...
+ 1
Depends how your compiler functions. Your first example also shows 0 on my system.
https://en.m.wikipedia.org/wiki/Undefined_behavior
0
thanks to all:-)