2
#include<stdio.h>
struct file{
        int a;
        int b;
        int (*fp) (int ,int);
        };
static int sum(int a, int b)
{
        return(a+b);
}
void main()
{
        struct file var;


        int sum1=0;

        var.fp=&sum;
        sum1=fp(2,4);
        printf("\nsum is  %d ",sum1);
}

how to call the function..?? i am getting an error called as undefined reference to fp..???

1
  • 1
    I'd recommend removing the linux tag on this post since linux has nothing to do with this question. Commented Dec 24, 2010 at 21:49

3 Answers 3

6

Since its a member of the structure you have to qualify it:

sum1 = var.fp( 2, 4 );
Sign up to request clarification or add additional context in comments.

Comments

6

You meant to say sum1 = var.fp(...) or sum1 = (*var.fp)(...) but you typed fp(...). C implicitly defined an external fp() for you to call. The compiler has to do this in order to compile legacy C code.

Use cc -Wall ... to generate errors for missing forward declarations.

Comments

2

Have you tried changing:

sum1=fp(2,4);

To:

sum1=var.fp(2,4);

?

1 Comment

Good. Don't forget to mark the answer to the question as "accepted" (the green check mark). Doing so will make it more likely that others will answer the questions you ask in the future.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.