T
Tomás Ó hÉilidhe
[Please forgive the introduction which talks about C++ rather than C, but
I'd just like to explain how I came to be writing if(p) before I go on to
tackle C-specific issues]
Coming originally from C++, I used to do the likes of the following,
using a pointer in a conditional:
void Func(int *p)
{
if (p)
{
*p++ = 7;
*p++ = 8;
}
}
In C++, the type of conditional expressions is bool. When a pointer
is converted to bool, it becomes false if it was a null pointer, true
otherwise. (Let's not get into the issue of whether a pointer to one past
the last element of an array can compare equal to null )
Now writing C code, I stopped and paused for a moment today when I
wrote code like that above. It looks to me, that, in the above code, the
pointer would be converted to an int before it would be evaluated by "if".
The compiler I'm using on the machine beside me is Microsoft Visual
Studio, so it's by no means guaranteed to be 100% compliant to any C
Standard. Anyway, it accepts the above function with zero errors and zero
warning. (And yes it's in C mode, not C++ mode).
But my question is:
1) Should a Standard-compliant compiler accept an implicit conversion
from pointer type to int? (as in "if (p)")
2) If so, what exactly is supposed to happen? Do null pointers become
zero while every other address becomes 1? Somehow I doubt that. My first
guess would be that the Standard doesn't define the behaviour of
converting pointers to integer types.
By the way, I'm aware that the inversion operator, i.e. !, turns a
null pointer into 1 and every other pointer into 0, but I'm talking
specifically about a bare naked pointer without an operator applied.
And yes, I'm fully aware that I can replace if (p) with if (!!p).
The exact code that had me thinking about this was:
assert(p);
I had a function which took a pointer and asserted straight away that
it actually pointed to something. When I switched to a different compiler
on a different machine, I got an error telling me there's no implicit
conversion from pointer to integer type (as I would hope!).
I think I can recall hearing some time ago that the argument to
assert MUST be of type int. Is this true? Should I be writing:
assert(!!p);
And again, what's supposed to happen when I do assert(p)? Is the
pointer converted to an int, and then the int evaluated to see if it's
true or false? If so, then what value does the resultant int have... ? My
guess is that it's undefined by the Standard.
I'd just like to explain how I came to be writing if(p) before I go on to
tackle C-specific issues]
Coming originally from C++, I used to do the likes of the following,
using a pointer in a conditional:
void Func(int *p)
{
if (p)
{
*p++ = 7;
*p++ = 8;
}
}
In C++, the type of conditional expressions is bool. When a pointer
is converted to bool, it becomes false if it was a null pointer, true
otherwise. (Let's not get into the issue of whether a pointer to one past
the last element of an array can compare equal to null )
Now writing C code, I stopped and paused for a moment today when I
wrote code like that above. It looks to me, that, in the above code, the
pointer would be converted to an int before it would be evaluated by "if".
The compiler I'm using on the machine beside me is Microsoft Visual
Studio, so it's by no means guaranteed to be 100% compliant to any C
Standard. Anyway, it accepts the above function with zero errors and zero
warning. (And yes it's in C mode, not C++ mode).
But my question is:
1) Should a Standard-compliant compiler accept an implicit conversion
from pointer type to int? (as in "if (p)")
2) If so, what exactly is supposed to happen? Do null pointers become
zero while every other address becomes 1? Somehow I doubt that. My first
guess would be that the Standard doesn't define the behaviour of
converting pointers to integer types.
By the way, I'm aware that the inversion operator, i.e. !, turns a
null pointer into 1 and every other pointer into 0, but I'm talking
specifically about a bare naked pointer without an operator applied.
And yes, I'm fully aware that I can replace if (p) with if (!!p).
The exact code that had me thinking about this was:
assert(p);
I had a function which took a pointer and asserted straight away that
it actually pointed to something. When I switched to a different compiler
on a different machine, I got an error telling me there's no implicit
conversion from pointer to integer type (as I would hope!).
I think I can recall hearing some time ago that the argument to
assert MUST be of type int. Is this true? Should I be writing:
assert(!!p);
And again, what's supposed to happen when I do assert(p)? Is the
pointer converted to an int, and then the int evaluated to see if it's
true or false? If so, then what value does the resultant int have... ? My
guess is that it's undefined by the Standard.