Is there a need to intiliaze a pointer to NULL in C? To elaborate:
int *ptr = NULL;
int *ptr;
Are these two lines equivalent?
Five answers:
Unca Alby
2013-03-10 20:46:09 UTC
Many compilers will default all declared variables to 0, and NULL is defined to be 0, so it will work in many cases. Other compilers will default everything except arrays and containers. Some compilers will default everything to whatever garbage was there before.
Many compilers will default the declared variables to 0 if they're GLOBAL. But inside a FUNCTION STACK, they won't. If you rely on the default in this case, you can get some really peculiar bugs that are difficult to figure out.
Therefore, it is NOT good practice to rely on the "default" feature. You're much better off setting the variable to an explicit value. That way you KNOW that the variable has been properly initialized.
Some compilers will even issue a warning if you don't. So just do it.
MichaelInScarborough
2013-03-09 19:12:19 UTC
No. They are not equivalent.
The first line is declaration AND definition of a variable,
the second is the declaration of a variable.
A pointer is pointing to an address area. If the pointer is declared, only usage of that pointer may make a program crash.
i.e.:
char name[] = "Michael";
char *ptr = name;
while(*ptr) putchar(*ptr++);
The pointer points to the first element of name. The content is checked for occurrence of the terminating 0 in the c string "Michael". If that is encountered, the while loop terminates.
Ghost
2013-03-09 19:10:03 UTC
Does it matter ?
Obviously if you have need of a pointer, you'll be getting it to point to something which will override whatever data (if any) is being held in *ptr with the address of the new variable anyway.
COREY
2013-03-09 19:12:00 UTC
It has been a while since I have taken the C++ course but from my understanding, you are at the beginning creating your variables and all of your variables have to be defined. I may be totally off, if so sorry but that is something I think I remember.
rieder
2016-10-30 02:00:05 UTC
An overloaded function is one that has the nicely suited comparable call and return type as yet another function yet has different numbers and/or varieties of parameters. the appropriate version of the function is named in line with what parameters you provide it, so on your function definitions you're able to rename all of them to easily print. by using the way, it is seen undesirable prepare to initialise a parameter in function brackets, you're able to initialise it exterior the brackets interior the main software then bypass it in. EDIT: Write "int length =10;" basically earlier your forward declarations of your applications i.e. void print(const int records[], int length = 10); void print(const choose for the flow records[], int length = 10); void print(const char records[], int length = 10); then you definately can replace them to: void print(const int records[], int length); void print(const choose for the flow records[], int length); void print(const char records[], int length) ; doing the comparable on your function definitions to boot. yet another tip : in case you progression the function definitions so as that they arrive earlier significant(), you could do away with the forward declarations. some human beings like having significant() first nevertheless, so while you're one among those persons then it would not truly count selection.
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.