HP C Programmer's Guide (92434-90009)

Chapter 5 141
Programming for Portability
Porting to ANSI Mode HP C
Function Prototype Considerations
There are three things to consider when using function prototypes:
Type differences between actual and formal parameters.
Declarations of a structure in a prototype parameter.
Mixing of const and volatile qualifiers and function prototypes.
Type Differences between Actual and Formal Parameters
When a prototype to a function is added, be careful that all calls to that function occur with
the prototype visible (in the same context). The following example illustrates problems
that can arise when this is not the case:
func1(){
float f;
func2(f);
}
int func2(float arg1){
/* body of func2 */
}
In the example above, when the call to func2 occurs, the compiler behaves as if func2 had
been declared with an old-style declaration int func2(). For an old-style call, the default
argument promotion rules cause the parameter f to be converted to double. When the
declaration of func2 is seen, there is a conflict. The prototype indicates that the parameter
arg1 should not be converted to double, but the call in the absence of the prototype
indicates that arg1 should be widened. When this conflict occurs within a single file, the
compiler issues an error:
Inconsistent parameter list declaration for "func2".
This error can be fixed by either making the prototype visible before the call, or by
changing the formal parameter declaration of arg1 to double. If the declaration and call of
func2 were in separate files, then the compiler would not detect the mismatch and the
program would silently behave incorrectly.
On HP-UX, the
lint(1)
command can be used to find such parameter inconsistencies
across files.
Declaration of a Structure in a Prototype Parameter
Another potential prototype problem occurs when structures are declared within a
prototype parameter list. The following example illustrates a problem that may arise:
func3(struct stname *arg);
struct stname { int i; };
void func4(void) {
struct stname s;
func3(s);
}
In this example, the call and declaration of func3 are not compatible because they refer to