Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
Is if(p), where p is a pointer, a valid and portable test?
Is if(p), where p is a pointer, a valid and portable test?
✍: Guest
It is always valid.
When C requires the Boolean value of an expression, a false value is inferred when the expression compares equal to zero, and a true value otherwise. That is, whenever one writes
if(expr)
where ``expr'' is any expression at all, the compiler essentially acts as if it had been written as
if((expr) != 0)
Substituting the trivial pointer expression ``p'' for ``expr'', we have
if(p) is equivalent to if(p != 0)
and this is a comparison context, so the compiler can tell that the (implicit) 0 is actually a null pointer constant, and use the correct null pointer value. There is no trickery involved here; compilers do work this way, and generate identical code for both constructs. The internal representation of a null pointer does not matter.
The boolean negation operator, !, can be described as follows:
!expr is essentially equivalent to (expr)?0:1
or to ((expr) == 0)
which leads to the conclusion that
if(!p) is equivalent to if(p == 0)
``Abbreviations'' such as if(p), though perfectly legal, are considered by some to be bad style (and by others to be good style;
2016-02-29, 1018👍, 0💬
Popular Posts:
Write an equivalent expression for x%8? x&7
How To Decrement Dates by 1? - MySQL FAQs - Introduction to SQL Date and Time Handling If you have a...
How To Control Table Widths? - XHTML 1.0 Tutorials - Understanding Tables and Table Cells Usually, b...
1. What is normalization. 2. Difference between procedure and functions. 3. Oracle 9i Vs 10g. 4. how...
The object that contains all the properties and methods for every ASP.NET page, that is built is .. ...