Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c中typedef结构中的函数指针_C - Fatal编程技术网

c中typedef结构中的函数指针

c中typedef结构中的函数指针,c,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 plan

我一直在写这段代码,不断遇到分段错误。我几乎可以肯定,它来自于我在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)
if((*(objs[i].intersect))(光线、objs[i]、&int_pt、&normal、&t)==1)

内存未分配给该指针,此时将出现分段错误

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


int(*相交)(光线、结构对象、VP_T*int_pt、VP_T*normal、双T)

所以。。如何初始化它?您可以打印这些值中的任何一个(包括func ptr)来检查“明显的不好”(如空指针)看起来您应该进行一些良好的旧调试。。。