HP C Programmer's Guide (92434-90009)

134 Chapter5
Programming for Portability
General Portability Considerations
Structure Assignment
The HP-UX C compilers support structure assignment, structure-valued functions, and
structure parameters. The structs in a struct assignment s1=s2 must be declared to be
the same struct type as in:
struct s s1,s2;
Structure assignment is in the ANSI standard. Prior to the ANSI standard, it was a BSD
extension that some other vendors may not have implemented.
Structure-Valued Functions
Structure-valued functions support storing the result in a structure:
s = fs();
All HP-UX implementations allow direct field dereferences of a structure-valued function.
For example:
x = fs().a;
Structure-valued functions are ANSI standard. Prior to the ANSI standard, they were a
BSD extension that some vendors may not have implemented.
Dereferencing Null Pointers
Dereferencing a null pointer has never been defined in any C standard. Kernighan and
Ritchie's The C Programming Language and the ANSI C standard both warn against such
programming practice. Nevertheless, some versions of C permit dereferencing null
pointers.
Dereferencing a null pointer returns a zero value on all HP-UX systems. The Series
700/800 C compiler provides the -z compile line option, which causes the signal SIGSEGV to
be generated if the program attempts to read location zero. Using this option, a program
can "trap" such reads.
Since some programs written on other implementations of UNIX rely on being able to
dereference null pointers, you may have to change code to check for a null pointer. For
example, change:
if (*ch_ptr != '\\0')
to:
if ((ch_ptr != NULL) &&*ch_ptr != '\\0')
Writes of location zero may be detected as errors even if reads are not. If the hardware
cannot assure that location zero acts as if it was initialized to zero or is locked at zero, the
hardware acts as if the -z flag is always set.
Expression Evaluation
The order of evaluation for some expressions will differ between HP-UX implementations.
This does not mean that operator precedence is different. For instance, in the expression:
x1 = f(x) + g(x) * 5;