HP C A.06.05 Reference Manual
Statements
return
Chapter 6 175
f2 = c; /* OK, quietly converts c to float */
return c; /* OK, quietly converts c to float */
}
Pointer Return Types
The C language is stricter about matching pointers. In the following example, f() is declared
as a function returning a pointer to a char. Some legal and illegal return statements are
shown.
char *f(void)
{
char **cpp, *cp1, *cp2, ca[10];
int *ip1, *ip2;
cp1 = cp2; /* OK, types match */
return cp2; /* OK, types match */
cp1 = *cpp; /* OK, types match */
return *cpp; /* OK, types match */
/* An array name without a subscript is converted
* to a pointer to the first element.
*/
cp1 = ca; /* OK, types match */
return ca; /* OK, types match */
cp1 = *cp2; /* Error, mismatched types */
/* (pointer to char vs. char) */
return *cp2; /* Error, mismatched types */
/* (pointer to char vs. char) */
cp1 = ip1; /* Error, mismatched pointer types */
return ip1; /* Error, mismatched pointer types */
return; /* Produces undefined behavior */
/* should return (char *) */
}
Note in the last statement that the behavior is undeļ¬ned if you return nothing. The only time
you can safely use return without an expression is when the function type is void.
Conversely, if you return an expression for a function that is declared as returning void, you
will receive a compile-time error.