We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi
Where function pointer stored? in stack , code segment , Data segment
for example in the following code where fp get stored? how can we know that where the pointer variable stored ?
#include <math.h> #include <stdio.h> // Function taking a function pointer as an argument double compute_sum(double (*funcp)(double), double lo, double hi) { double sum = 0.0; // Add values returned by the pointed-to function '*funcp' for (int i = 0; i <= 100; i++) { double x, y; // Use the function pointer 'funcp' to invoke the function x = i/100.0 * (hi - lo) + lo; y = (*funcp)(x); sum += y; } return sum; } int main(void) { double (*fp)(double); // Function pointer double sum; // Use 'sin()' as the pointed-to function fp = &sin; sum = compute_sum(fp, 0.0, 1.0); printf("sum(sin): %f\n", sum); // Use 'cos()' as the pointed-to function sum = compute_sum(&cos, 0.0, 1.0); printf("sum(cos): %f\n", sum); return 0; }
thanks in advance goran
In this case, it is stored on the stack.
...or a register.