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.
Well, you could try addition/subtraction on it, although that invokes
undefined behaviour.
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.
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.