2

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.

Community
  • 1
  • 1
covstat
  • 331
  • 1
  • 3
  • 10

1 Answers1

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);
ILYA Khlopotov
  • 705
  • 3
  • 5