printf() error with long double and null pointer.

F

Flash Gordon

Not completely true. You could use an unsigned char pointer to read the
bytes of its representation one at a time and print them.
I'd say "null pointer" - "NULL pointer" is not technically correct, but
i knew what you meant. How do you think you can "mathematically
manipulate" it? multiply or divide it by something? take its logarithm
base 10?

Well, you could try addition/subtraction on it, although that invokes
undefined behaviour.
you mean if I

if(0==ptr)

then 0 is cast to a pointer of same type as ptr? I would expect that.

Hypothetical situation :

a char * which needs to point to memory address 0. What is the
thoughts on this? In this case the pointer must "hold 0" but is not
necessarily a NULL pointer?

Conversion of an integer (i.e. an integer that is not a constant
expression equal to 0) to a pointer is implementation-defined.

If you really needed this, an implementation would probably go about it
by having the internal representation of a null pointer be some other
value, and you'd do (char *)(int)0 [with the cast to int to make the 0
no longer be a null pointer constant].

If you are talking about accessing a HW register or something hard coded
at address 0 then yes, that might be used. Or some other system
dependent memory. If, on the other hand, address 0 is just a normal
address and the compiler happens to put a char array at that address,
you would just access it as you would any other char array.

I.e.,

#include <stdio.h>

int main(void)
{
char fred[] = "my string";
/* compiler places fred at location 0 */
char *derf=0; /* derf does *not* point to fred */
if (derf == fred)
puts("This never happens.");
derf = fred; /* derf now contains address 0 */
return 0;
}

Basically, 0 in the source code is a "magic" symbol. When used in a
pointer context it does *not* mean 0, it means "null pointer constant".
It just happens to be spelt "0".

Another regular here has said he wishes C had a keyword "nil" as the one
and only null pointer constant, and I agree. Where this the case you
could not assign 0 to a pointer or compare a pointer with 0 (except by
using a cast) and the confusion would go away.
 
R

Richard Tobin

gcc, on the other hand, uses the Intel 80-bit extended precision type
for long double.

That seems like a really bad decision on a system whose libraries use
64-bit long doubles. Microsoft's decision may have been unwise, but
it's Mingw's decision that breaks correct programs. As you can see
from this thread, it has the potential to (at the very least) waste
a lot of people's time.

-- Richard
 
R

Richard Bos

Richard G. Riley said:
Would you agree that a "null pointer" had a value of NULL?

No. NULL is a macro. A null pointer has a null value.

Many of the problems newbies seem to have with null pointers, and
particularly with null pointers in combination with 0, seem to me to
stem from a confusion between a source code and the running executable;
that is, between a null pointer constant and a pointer object which
happens to have a null value. That is why it is IMO important not to
confuse the issue any more by claiming that null pointers have either
zero, or NULL value, or that NULL is zero (while keeping in mind that
NULL _may_ be a (source-level!) 0, but need not be).

Richard
 
R

Richard G. Riley

No. NULL is a macro. A null pointer has a null value.

I must remember to be more careful

that should read:

" .. had a value of null"
Many of the problems newbies seem to have with null pointers, and
particularly with null pointers in combination with 0, seem to me to

Well, I'm certainly no newbie but would admit to taking some short
cuts in the past with regard to null pointers. Not something I would
be overly proud avout, but something very prevalent in C systems the
world over.
stem from a confusion between a source code and the running executable;
that is, between a null pointer constant and a pointer object which
happens to have a null value. That is why it is IMO important not to
confuse the issue any more by claiming that null pointers have either
zero, or NULL value, or that NULL is zero (while keeping in mind that
NULL _may_ be a (source-level!) 0, but need not be).

Richard

What is a null pointer to you? In standard "conversation" it is for me
a pointer which has a null value or ptr==NULL or (!ptr);
 
P

pete

that should read:

" .. had a value of null"

The meaning of "null" is not sufficiently distinct from
the meaning of "zero", in order for the meaning of
"a value of zero" to be different from the meaning of
"a value of null".
 
R

Richard G. Riley

The meaning of "null" is not sufficiently distinct from
the meaning of "zero", in order for the meaning of
"a value of zero" to be different from the meaning of
"a value of null".

Yes. I know. We covered all that. But a pointer has a value. Its
something. So NULL is out of the question because it's a macro
..... zero, because it isnt't, and now null because its not
sufficiently distinct? Do I detect a vicious circle here again?

Tell me, what terminology would you use to describe a pointer that has
just had NULL assigned to it?

Or did I misunderstsnd your meaning?
 
R

Robin Haigh

Richard G. Riley said:
I must remember to be more careful

that should read:

" .. had a value of null"


Well, I'm certainly no newbie but would admit to taking some short
cuts in the past with regard to null pointers. Not something I would
be overly proud avout, but something very prevalent in C systems the
world over.


What is a null pointer to you? In standard "conversation" it is for me
a pointer which has a null value or ptr==NULL or (!ptr);

This is absolute. But null pointer constants are magic. They violate the
normal rules of expression evaluation. In a world with null pointer
constants, we can have
(char *)expr1 != (char *)expr2
even though expr1 == expr2. And if ptr == expr it's not necessarily safe to
say that the value of ptr is the same as the value of expr. You have to
pick your way through the minefield very delicately
 
P

pete

Richard said:
Yes. I know. We covered all that. But a pointer has a value. Its
something. So NULL is out of the question because it's a macro
.... zero, because it isnt't, and now null because its not
sufficiently distinct? Do I detect a vicious circle here again?

Tell me, what terminology would you use to describe a pointer that has
just had NULL assigned to it?

It's a "null pointer".
 
R

Richard Bos

Richard G. Riley said:
I must remember to be more careful

that should read:

" .. had a value of null"

In that case, no; I wouldn't complain if someone else called it that,
but I wouldn't phrase it like that myself.
Well, I'm certainly no newbie but would admit to taking some short
cuts in the past with regard to null pointers. Not something I would
be overly proud avout, but something very prevalent in C systems the
world over.

I certainly have done so myself, when I was considerably more of a
newbie than I was now.
What is a null pointer to you? In standard "conversation" it is for me
a pointer which has a null value or ptr==NULL or (!ptr);

Yes; but a NULL is something else. A null pointer _compares_ equal to
both NULL and zero - any mathematical zero, including 0.0!), but that's
only because it's an explicit exception, and NULL, where it is literally
a 0, is converted to a null pointer - _not_ the other way 'round.

Richard
 
J

Jordan Abel

Yes; but a NULL is something else. A null pointer _compares_ equal to
both NULL and zero
yes.

- any mathematical zero, including 0.0!)

No. 0.0 is not a null pointer constant.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top