#include <math.h>
#include <stdio.h>
float function_simple(float const, float const);
float function_args_ptr(float const *const, float const *const);
void function_result_ptr(float const, float const, float *const);
void function_all_ptr(float const *const, float const *const, float *const);
int main(int argc, char const *const *argv) {
float a = 3.f, b = 1.2f, y = 0.f,
*a_ptr = &a,
*b_ptr = &b,
*y_ptr = &y;
{
float (*f)(float const, float const)
= function_simple;
y = f(a, b); // or
y = f(*a_ptr, *b_ptr); // or
*y_ptr = f(a, b); // or
*y_ptr = f(*a_ptr, *b_ptr);
printf_s("y : %f,\t f addr : %p\r\n", y, f);
} {
float (*f)(float const *const, float const *const)
= function_args_ptr;
y = f(&a, &b); // or
y = f(a_ptr, b_ptr); // or
*y_ptr = f(&a, &b); // or
*y_ptr = f(a_ptr, b_ptr);
printf_s("y : %f,\t f addr : %p\r\n", y, f);
} {
void (*f)(float const, float const, float *const)
= function_result_ptr;
f(a, b, &y); // or
f(*a_ptr, *b_ptr, &y); // or
f(a, b, y_ptr); // or
f(*a_ptr, *b_ptr, y_ptr);
printf_s("y : %f,\t f addr : %p\r\n", y, f);
} {
void (*f)(float const *const, float const *const, float *const)
= function_all_ptr;
f(&a, &b, &y); // or
f(a_ptr, b_ptr, &y); // or
f(&a, &b, y_ptr); // or
f(a_ptr, b_ptr, y_ptr);
printf_s("y : %f,\t f addr : %p\r\n", y, f);
}
return 0;
}
float function_simple(float const a, float const b) {
return 5.f / (a * a) + tan(b * b);
}
float function_args_ptr(float const *const a, float const *const b) {
return function_simple(*a, *b);
}
void function_result_ptr(float const a, float const b, float *const result) {
*result = function_simple(a, b); // or
// *result = function_args_ptr(&a, &b);
}
void function_all_ptr(float const *const a, float const *const b, float *const result) {
*result = function_simple(*a, *b); // or
// *result = function_args_ptr(a, b); // or
// function_result_ptr(*a, *b, result);
}