This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Where function pointer stored?

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

0