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

5th Jun 2018, 9:24 AM
Param🇮🇳
Param🇮🇳 - avatar
8 Answers
+ 7
which compiler you use??
5th Jun 2018, 11:10 AM
Scooby
Scooby - avatar
+ 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.
5th Jun 2018, 9:52 AM
Scooby
Scooby - avatar
+ 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
5th Jun 2018, 10:19 AM
Scooby
Scooby - avatar
+ 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
5th Jun 2018, 12:29 PM
Max
Max - avatar
+ 1
you are saying an uninitialed pointer contains NULL value but because of turboc++ ,NULL value is showed as 0 !!!!
5th Jun 2018, 10:12 AM
Param🇮🇳
Param🇮🇳 - avatar
+ 1
it generated error , invalid memory reference...
5th Jun 2018, 11:08 AM
Param🇮🇳
Param🇮🇳 - avatar
+ 1
Depends how your compiler functions. Your first example also shows 0 on my system. https://en.m.wikipedia.org/wiki/Undefined_behavior
5th Jun 2018, 12:27 PM
Vlad Serbu
Vlad Serbu - avatar
0
thanks to all:-)
5th Jun 2018, 3:19 PM
Param🇮🇳
Param🇮🇳 - avatar