Typecasting a pointer to NULL

P

prix prad

Hi,
I have the foll. program. Shoudn't the program crash when the
NULL pointer is typecast to structure and we try to get the address
of one of its members.

I mean how is '&p->c' supposed to work when p is NULL?

<snip>
#include <stdio.h>
typedef struct temp
{
int f;
int k;
} TEMP;
typedef struct abc_
{
int a;
TEMP b;
int c;
} abc;

main() {
void *q = NULL;
abc *p = NULL;
int i = (int) (&((abc *)NULL)->c);
int j = (int) &((abc *)0)->c;
int k = (int) &((abc *)q)->c;
int l = (int) &p->c;
printf("\n i is %d, j is %d, k is %d, l is %d, m is %d", i, j, k,
l, &p);
printf("\n Hi: Size of abc is %d\n ", sizeof(abc));
return (1);
}
</snip>

The o/p is
i is 12, j is 12, k is 12, l is 12, m is -4195224
Hi: Size of abc is 16
 
F

Flash Gordon

prix said:
Hi,
I have the foll. program. Shoudn't the program crash when the
NULL pointer is typecast to structure and we try to get the address
of one of its members.

What guarantee is there that if you walk in the fast lane of a motorway
you will get hit by a car? Undefined behaviour is like that, sometimes
it causes a major incident, other times nothing untoward seems to happen.

Yes, I *have* walked in the fast lane of a motorway, but that was
because the motorway was blocked so no vehicles were moving.
I mean how is '&p->c' supposed to work when p is NULL?

<snip code>

It isn't supposed to work, nor is it supposed to crash. It is undefined
behaviour and anything can happen. Note though that in this case after
the dereference you then take the address, so one possible result is the
two operators cancelling out.

I'm guessing you have been looking at either the definition of the
standard offsetof macro, or someones attempt to write its equivalent.
Look up the offsetof macro in your C reference and use that (or tell the
author of the code to use it) when that is what you want!
 
C

CBFalconer

prix said:
I have the foll. program. Shoudn't the program crash when the
NULL pointer is typecast to structure and we try to get the
address of one of its members.

No. Casting NULL to another pointer type results in NULL.
I mean how is '&p->c' supposed to work when p is NULL?

It isn't. It didn't. It won't.
 
J

James Kuyper

CBFalconer said:
No. Casting NULL to another pointer type results in NULL.

No, it results in a null pointer of the specified type. That null
pointer must compare equal to NULL, but it is not NULL. NULL is the name
of a standard macro; null is an adjective that can be applied to (among
other things) pointer values. Please keep them distinct.
 
B

Barry Schwarz

That's a surprise since

sizeof(&p->c) works just fine....

Chuck may be a pain but claiming that success using a non-evaluated
expression implies success when the expression is evaluated is just
technically inaccurate. The code in the OP attempts to dereference a
null pointer and Chuck is correct that it doesn't work, regardless of
the results.
 
C

CBFalconer

James said:
No, it results in a null pointer of the specified type. That
null pointer must compare equal to NULL, but it is not NULL.
NULL is the name of a standard macro; null is an adjective
that can be applied to (among other things) pointer values.
Please keep them distinct.

Correction accepted. I do tend to leave out the (unimportant to
me) details.
 

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,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top