User`s manual

Dynamic C Users Manual digi.com 39
You can pass arguments to functions that are called indirectly by pointers, but the compiler will not check
them for correctness. This means that the auto promotions provided by Dynamic C type checking will not
occur, so values must be cast to the type that is expected or the size may not be correct. For example, if a
function takes a long as a parameter, and you pass it a 16-bit integer value, it must be cast to type long in
order for 4 bytes to be put onto the stack.
The following program shows some examples of using function pointers.
4.17 Argument Passing
In C, function arguments are generally passed by value. That is, arguments passed to a C function are gen-
erally copies on the program stack of the variables or expressions specified by the caller. Changes made to
these copies do not affect the original values in the calling program.
In Dynamic C and most other C compilers, however, arrays are always passed by address. This policy
includes strings (which are character arrays).
Dynamic C passes structs by value on the stack. Passing a large struct takes a long time and can
easily cause a program to run out of memory. Pass pointers to large structs if such problems occur.
For a function to modify the original value of a parameter, pass the address of, or a pointer to, the parame-
ter and then design the function to accept the address of the item.
typedef int (*fnptr)(); // create pointer to function that returns an integer
main(){
int x,y;
int (*fnc1)(); // declare var fnc1 as a pointer to an int function.
fnptr fp2; // declare var fp2 as pointer to an int function
fnc1 = intfunc; // initialize fnc1 to point to intfunc()
fp2 = intfunc; // initialize fp2 to point to the same function.
x = (*fnc1)(1,2); // call intfunc() via fnc1
y = (*fp2)(3,4); // call intfunc() via fp2
printf("%d\n", x);
printf("%d\n", y);
}
int intfunc(int x, int y){
return x+y;
}