2015-10-26 67 views
-5

我一直在研究这段代码,并且遇到了一个段错误。我几乎肯定它来自于我如何在OBJ_T类型中初始化我的功能点。下面是我有:函数指针在c中的typedef结构中

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; 

这是该行我得到的赛格故障:

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

那么..你如何初始化它? –

+0

你可以打印任何这些值(包括func ptr)来检查“明显不良”(如空指针)看起来你应该做一些很好的旧调试... – John3136

回答

0

如果((*(OBJ文件[I] .intersect))(射线,OBJ文件[I] & int_pt,& normal,& t)== 1)

内存没有分配给那个指针,那时候你会得到分段错误。

要解决这个问题,首先必须为结构成员分配内存。 (* intersect)(RAY_T ray,struct obj,VP_T * int_pt,VP_T * normal,double t);