开发者

function pointer usage in c

开发者 https://www.devze.com 2023-01-31 22:40 出处:网络
#include<stdio.h> struct file{ int a; 开发者_开发知识库int b; int (*fp) (int ,int); }; static int sum(int a, int b)
#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..???


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

sum1 = var.fp( 2, 4 );


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.


Have you tried changing:

sum1=fp(2,4);

To:

sum1=var.fp(2,4);

?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号