have a C doubt ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

have a C doubt ?

is int *ptr = NULL ; same as int *ptr = 0 ;

12th Jun 2020, 1:20 PM
NIK
NIK - avatar
3 Answers
+ 1
There is no nullptr type in C. nullptr is a C++ feature that was added to prevent you from calling the wrong overloaded function when one took an integer and the other a pointer argument. A 0-as-null argument causes ambiguity, is it a null pointer or an integer? The nullptr cleared up that confusion by always calling the pointer function. The second reason is that C++ doesn't type promote void pointers so the (void *)0 was not an option. NIK In C void pointers are automatically type promoted and there is no function overloading so NULL is fine. In C++ it's better to use nullptr due to the reasons above. You should use NULL for pointers, 0 for integers and '\0' for characters by convention. It shows what variable type you're working with and it's less confusing.
12th Jun 2020, 9:40 PM
Gen2oo
Gen2oo - avatar
0
With pointers you should use nullptr int *ptr = nullptr; You can also use 0 or NULL , but nullptr is for pointers and I won't be cause an error in some situations .
12th Jun 2020, 2:23 PM
꧁༒☬Bad☬Boy☬༒꧂
꧁༒☬Bad☬Boy☬༒꧂ - avatar
0
so they are the same but can have undefined behavior thats what u are saying ?
12th Jun 2020, 3:14 PM
NIK
NIK - avatar