Is const void* p the same as a null pointer? Why would i use it?

Recommended Answers

All 15 Replies

Is const void* p the same as a null pointer? Why would i use it?

No, that's a pointer named p. You might use it for holding the memory address of something. For example, memcpy takes a const void* argument.

A NULL pointer is a pointer that points to address 0

const void *p = 0;

Address zero would not be an absolute address, or is it? Otherwise, what does it mean? I am confused now.

>A NULL pointer is a pointer that points to address 0
Bad dragon, now you're just confusing people. A null pointer doesn't have to refer to a specific address, and just because 0 is a null pointer constant doesn't mean it points to address 0.

just because 0 is a null pointer constant doesn't mean it points to address 0.

You are splitting hairs -- the memory location of a pointer contains an address. A NULL pointer ALWAYS has absolute address 0 in C and C++ programs. Its up to the implementation to evaluate that address as appropriate for the operating systems. There are many references to that, here's just one of them, including Bjarne Stroustrup

http://www.eskimo.com/~scs/C-faq/q5.2.html

Is there a difference between NULL and '\0'

Sometimes there is a difference in C, but not in C++. C compilers often define NULL as (void *)0 or (char *)0, but all standard C++ compliant compilers define NULL as 0. For that reason, NULL is never (supposed) to be applied to integers, just pointers.

int n = NULL; <<< WRONG

Sometimes there is a difference in C, but not in C++. C compilers often define NULL as (void *)0 or (char *)0, but all standard C++ compliant compilers define NULL as 0. For that reason, NULL is never (supposed) to be applied to integers, just pointers.

int n = NULL; <<< WRONG

Sorry...i guess u r not getting me.....
i asked NULL and '\0'.....or does it mean '\0' and 0 are same

.or does it mean '\0' and 0 are same

Yes, '\0' and 0 are the same.

>There are many references to that, here's just one of them
Neither of those links (which I've seen before) agree with your claim about a null pointer pointing to an absolute address 0. 0 is a null pointer constant but has nothing to do with the value (absolute or not) of a null pointer. It's just that 0 makes more sense than 523 or 'Q', both of which could just as easily be used as a null pointer constant. I'm not splitting hairs because you're encouraging a common and annoying misconception about null pointers. However, if you want to get the best and brightest from comp.lang.c to say that you're right and I'm wrong, I'll be happy to appologize.

>Is there a difference between NULL and '\0'
Technically, in C++, no. They both evaluate to 0. However, mixing them up can be dreadfully confusing to a reader because NULL refers to a pointer context and '\0' refers to a character context. They're not logically interchangeable. In C, you would be wise not to mix them up because NULL is often defined as ((void *)0), which is most certainly not a character type.

I understand the confusion because I said "A NULL pointer ALWAYS has absolute address 0" which is incorrect. The pointer does not have an address of 0, its the value stored at the pointer's address which is 0. So I apologize for any confusion this may have caused. The term "null pointer" always referrs to the value stored at the pointer's address, which is 0 when NULL.

>The term "null pointer" always referrs to the value stored at the pointer's address, which is 0 when NULL.
That's still not correct as it strongly implies that a null pointer has the value 0, which is all bits zero, which is not necessarily the case. You really should read the rest of the C FAQ that you so kindly linked to earlier in this thread. It covers this issue adequately.

If 0 is good enough for Bjarne, then it's good enough for me. Its up to the compiler to interpret that 0 for the target operating system -- programmers just need to put a 0 there. The language would not be very portable if the programmer had to worry about whether or not any given implementation did or did not recognize address 0 as a valid address, or even if it had an address 0. That's the compiler's job.

>If 0 is good enough for Bjarne, then it's good enough for me.
0 is good enough because that's how the language works, duh. But saying that a null pointer has a 0 value displays a fundamental confusion between the abstract and the concrete. In the abstract, 0 is a null pointer constant. But in the concrete, you don't know what the actual value of a null pointer is.

>programmers just need to put a 0 there
Correct, but a lot of programmers still want to know how things work under the hood, even if an abstract understanding is enough for you.

I think I already mentioned it is up to the compiler to interpret null pointers for the destination operating system. So I don't see any differences of opinion. you are making a really trivel distinction that most of us mortals don't need to worry about. True, its not trivel to an embedded programmer, but this thread is not about embedded programming.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.