Are these three equivalent:
char* p= NULL;
char* q = "";
char r[] = {'\0'};
I suspect the first one is different from the rest, but I'm not entirely sure.
I'm answering for C++ even though the OP has also tagged the question as C. These are two different languages. It's not a good idea to conflate them.
This declaration:
char* q = "";
used a deprecated conversion in C++03, and became invalid in C++11. We're now at C++14.
These two declarations:
char* p= NULL;
char r[] = {'\0'};
are fundamentally different. The first declares a pointer and sets it null. The second declares an array of one item, which item is set to null.
Regarding
” Are these three equivalent
the answer is no, not at all: one is just invalid, one declares a pointer, one declares an array.
char* p = NULL;
This assigns NULL to the pointer p
which means that p
does not point to any valid memory address.
char* q = "";
char r[] = {'\0'};
These both create empty strings and are basically equivalent. q
points to a valid memory address, unlike p
in the previous example. r
is an array with an empty string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With