Recasting *void function arguments

好久不见. 提交于 2019-12-20 04:07:29

问题


I posted a question here earlier that I think I can answer if someone can help me with the following:

I have a function

double func(void* data)

I want to pass in an object or struct. (In my case an armadillo matrix or even just and std::vector).

How do I pass a pointer to an object as an argument to func() and then, once inside func(), how do I recast the void pointer into its original type?

Edit: Here's what ended up working, where mat is the Armadillo matrix class:

mat A(2,2);
A << 1 << 2 << endr << 3 << 4; // A=[1,2; 3,4]
func(&A);

and in func:

double func(void* data) {
   mat* pB = (mat*)(data);
   mat B = pB[0];
}

The matrix B and A now contain the same data.


回答1:


If I understand you correctly you need something like this.

double func(void* data_v) {
  struct my_type * data = data_v;
}

func((void*)my_data);


来源:https://stackoverflow.com/questions/8435933/recasting-void-function-arguments

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!