-5

I have to use void** in a program. I am writing the following code. please guide me where I am wrong.

struct kdnode
{
 kdnode* lch;
 int k;
 void **dataptr;
 kdnode* rch;
}; 

then I am assigning

kdnode rt;
rt.dataptr=new void*[k];
rt.dataptr[0]=new int;

there was also this dereferencing involved:

*(rt->dataptr[0])=n; //n is an initialized integer value.

basically I want to assign the elements of the array of void pointers to pointers of different datatypes. As the compiler is throwing error :

void* is not a pointer-to object type

Please guide me what to do.

  • Why aren't you using standard-library containers like `std::vector` for this? – tadman Sep 03 '14 at 20:57
  • Maybe OP is interfacing with some C API? – cdhowie Sep 03 '14 at 20:57
  • Nothing illegal in the code you posted, except perhaps for the missing definition of `k`. Are you trying to dereference `rt.dataptr[0]` somewhere? – T.C. Sep 03 '14 at 20:58
  • 1
    Compilation errors are printed, not thrown. Exceptions are thrown. – user207421 Sep 03 '14 at 20:59
  • 7
    First thing that's wrong: using `void**` in C++. – aschepler Sep 03 '14 at 20:59
  • 2
    Avoid void pointers. You lose type safety. – Ed Heal Sep 03 '14 at 21:01
  • Possible duplicate: http://stackoverflow.com/questions/7949761/c-error-void-is-not-a-pointer-to-object-type – djhaskin987 Sep 03 '14 at 21:02
  • The error would make sense if the code in your question isn't actually the code you're using. If there's a typo in the data types, and you've actually got a `void *` instead of `void **`. Can you please provide a complete minimal program that doesn't compile, that you think should? –  Sep 03 '14 at 21:03
  • Works for me here: http://ideone.com/LBZCuT – PaulMcKenzie Sep 03 '14 at 21:05
  • What compiler, what line number, and can you add to question the smallest possible compilation unit (file), without headers that will produce this error? – ctrl-alt-delor Sep 03 '14 at 21:07

1 Answers1

0

I can reproduce this error only if I add something like

*rt.dataptr[0] = 1;

With the addition, g++ complains:

main.cpp:13:14: error: ‘void*’ is not a pointer-to-object type
 *rt.dataptr[0] = 1;

You can't dereference a void *. Cast it back to the original type (int * in this case) if you want to dereference it.

T.C.
  • 133,968
  • 17
  • 288
  • 421
  • FWIW, my guess for the typo was that the OP actually has `void *dataptr;` instead of `void **dataptr;`. –  Sep 03 '14 at 21:05