HP C Programmer's Guide (92434-90009)
Chapter 5 129
Programming for Portability
General Portability Considerations
As another example, if a program passes a casted pointer to a function that expects a
parameter with stricter alignment, an alignment fault may occur. For example, the
following program causes an alignment fault on Series 700/800:
void main (int argc, char *argv[])
{
char pad;
char name[8];
intfunc((int *)name[1]);
}
int intfunc (int *iptr)
{
printf("intfunc got passed %d\n", *iptr);
}
Type Incompatibilities and typedef
The C typedef keyword provides an easy way to write a program to be used on systems
with different data type sizes. Simply define your own type equivalent to a provided type
that has the size you wish to use.
For example, suppose system A implements int as 16 bits and long as 32 bits. System B
implements int as 32 bits and long as 64 bits. You want to use 32 bit integers. Simply
declare all your integers as type INT32, and insert the appropriate typedef on system A:
typedef long INT32;
The code on system B would be:
typedef int INT32;
Conditional Compilation
Using the #ifdef C preprocessor directive and the predefined symbols __hp9000s300,
__hp9000s700, and __hp9000s800, you can group blocks of system-dependent code for
conditional compilation, as shown below:
#ifdef __hp9000s300
…
Series 300/400-specific code goes here...
…
#endif
#ifdef __hp9000s700
…
Series 700-specific code goes here...
…
#endif
#ifdef __hp9000s800
…
Series 700/800-specific code goes here...