Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.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++ 指向struct&;的指针;函数指针->;分段断层_C++_C - Fatal编程技术网

C++ 指向struct&;的指针;函数指针->;分段断层

C++ 指向struct&;的指针;函数指针->;分段断层,c++,c,C++,C,在执行这个测试程序的过程中,我总是遇到一个分段错误。我不明白为什么。也许有人能给我解释一下,我肯定我把指针的东西弄混了 #include <stdio.h> struct xy { int (*read)(); void (*write)(int); }; struct z { struct xy *st_xy; }; static void write_val(int val) { printf("writ

在执行这个测试程序的过程中,我总是遇到一个分段错误。我不明白为什么。也许有人能给我解释一下,我肯定我把指针的东西弄混了

#include <stdio.h>

struct xy {
    int         (*read)();
    void        (*write)(int);
};

struct z {
    struct xy    *st_xy;
};


static void write_val(int val)
{
    printf("write %d\n", val);
}

static int read_val()
{
    /* return something just for testing */
    return 100;
}

int init(struct xy *cfg)
{
    cfg->read = read_val;
    cfg->write = write_val;
    return 0;
}

int reset(struct z *st_z)
{
    /* write something just for testing */
    st_z->st_xy->write(111);

    return 55;
}

int main(int argc, char **argv)
{
    static struct z test;
    int ret;
    int ret2;

    ret = init(test.st_xy);
    printf("init returned with %d\n", ret);

    ret2 = reset(&test);
    printf("reset returned with %d\n", ret2);

    return 0;
}
#包括
结构xy{
int(*读)();
无效(*写)(int);
};
结构z{
结构xy*st_xy;
};
静态无效写入值(int val)
{
printf(“写入%d\n”,val);
}
静态int read_val()
{
/*返回只是为了测试的东西*/
返回100;
}
int init(结构xy*cfg)
{
cfg->read=read\u val;
cfg->write=写入值;
返回0;
}
整数重置(结构z*st_z)
{
/*写些测试用的东西*/
st_z->st_xy->写入(111);
返回55;
}
int main(int argc,字符**argv)
{
静态结构z检验;
int ret;
int ret2;
ret=初始值(测试标准xy);
printf(“init返回%d\n”,ret);
ret2=重置(和测试);
printf(“重置返回%d\n”,ret2);
返回0;
}

您从未分配实际的
xy
对象。您的
test.st_xy
只是一个垃圾指针,不允许取消引用

相反,可以这样做:

 static struct z test;
 static struct xy inner_test;
 test.st_xy = &inner_test;

 // ...

 ret = init(test.st_xy);

您从不分配实际的
xy
对象。您的
test.st_xy
只是一个垃圾指针,不允许取消引用

相反,可以这样做:

 static struct z test;
 static struct xy inner_test;
 test.st_xy = &inner_test;

 // ...

 ret = init(test.st_xy);

将指向xy的未初始化指针传递给init函数

init(test.st_xy);
st_xy尚未初始化。我认为Stuxy没有必要成为一个指针

struct z {
   struct xy st_xy;
};

int main(int argc, char **argv)
{
  static struct z test;
  init(&test.st_xy);
}

将指向xy的未初始化指针传递给init函数

init(test.st_xy);
st_xy尚未初始化。我认为Stuxy没有必要成为一个指针

struct z {
   struct xy st_xy;
};

int main(int argc, char **argv)
{
  static struct z test;
  init(&test.st_xy);
}

您尚未初始化test.st_xy
ret=init(test.st_xy)st_xy是指向结构的指针,但它从未初始化@David Heffernan抱歉,我忘了接受答案,现在我接受了。对于信息链接,您尚未初始化test.st_xy
ret=init(test.st_xy)st_xy是指向结构的指针,但它从未初始化@David Heffernan抱歉,我忘了接受答案,现在我接受了。信息链接谢谢,谢谢,谢谢。我看了代码好几次,但没有发现xy的缺失分配@如果答案是有帮助的,那就接受它吧。啊啊,谢谢。我看了代码好几次,但没有发现xy的缺失分配@如果答案是有帮助的,那就接受它吧。