c中typedef结构中的函数指针

时间:2015-10-26 23:52:51

标签: c

我一直在研究这段代码并不断遇到分段错误。我几乎肯定它来自我在OBJ_T类型中初始化我的功能点的方式。这就是我所拥有的:

typedef struct obj
{
    COLOR_T color;
    int (*intersect)(RAY_T ray, struct obj, VP_T *int_pt, VP_T *normal, double *t);

    union geom
    {
       SPHERE_T sphere;
       PLANE_T plane;
    }geom;

} OBJ_T;

这是我得到seg错误的一行:

if((*(objs[i].intersect))(ray, objs[i], &int_pt, &normal, &t) == 1)

1 个答案:

答案 0 :(得分:0)

if((*(objs [i] .intersect))(ray,objs [i],& int_pt,& normal,& t)== 1)

当您获得分段错误时,不会为该指针分配内存。

要解决这个问题,首先必须为结构成员分配内存。

int(* intersect)(RAY_T ray,struct obj,VP_T * int_pt,VP_T * normal,double t);

相关问题