1
struct limit{
  int up;
  int down;
};

void *x;

struct limit *l;
l->up=1;
l->down=20;

x=l;

cout<<x->up;

This is part of my code I am getting error in last line ‘void*’ is not a pointer-to-object type. I know last line in my code is wrong. I just want to know how to print up and down values using x variable.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Sri Harsha
  • 641
  • 2
  • 8
  • 18
  • 5
    You can't access any members through a `void` pointer. Why do you want to do this? Can you provide some context? – Björn Pollex Oct 16 '13 at 12:58
  • 5
    You have to _cast_ `x`. If you don't know what type-casting is, you should get some learning resources. – Zeta Oct 16 '13 at 12:58
  • 5
    Note, you are using `l` uninitialized in the code above. – Shafik Yaghmour Oct 16 '13 at 13:00
  • 2
    The compiler message actually says it all. Since `void*` isn't a pointer-to-object-type (as little as e.g. `int` or `bool` would be) you can't access any of its formally non-existing members. It really doesn't matter whether -- by accident -- the `void` pointer points at some structure that has those members. – Damon Oct 16 '13 at 13:06
  • Yep, explain why using `void *`? – zoska Oct 16 '13 at 15:46
  • i using pthreads. in that i should pass a arg of type void * we should pass only one arg so i want to pass struct so that i can get multiple values at a time @zoska – Sri Harsha Oct 16 '13 at 16:31
  • show us exact example of using pthreads in which you want to use void *, so we can exactly direct you in the right way. – zoska Oct 17 '13 at 14:44

1 Answers1

5

In this part:

struct limit *l;
l->up=1;
l->down=20;

you are dereferencing uninitialized pointer l, which results in undefined behavior. However, even if you initialized it properly, after you assign it to void*, you can not dereference void pointer:

void* x = l;
cout<< x->up;

you need to explicitly cast it back to struct limit*:

void* x = l;
struct limit * y = static_cast<struct limit*>(x);
cout << y->up;

or yet even better: avoid using void* at first place.


Since you mentioned that you're doing this because of , then this answer will help you :)

Community
  • 1
  • 1
LihO
  • 41,190
  • 11
  • 99
  • 167
  • Instead of **static_cast** we can use **(limit *)**. It works – Sri Harsha Oct 17 '13 at 09:45
  • @rock: In this case (+ assuming this is C++), `static_cast` is more reasonable than C-style cast :) – LihO Oct 17 '13 at 10:15
  • we can used mixed c and c++ :) – Sri Harsha Oct 17 '13 at 11:19
  • After reading OP comment about using pthreads, I think he is having problems with retrieving void* data through thread function. If you could make some additional changes, then this answer should cover his problem :). – zoska Oct 17 '13 at 14:48
  • @zoska: I've added a link to related question instead :) – LihO Oct 17 '13 at 14:54
  • @rock: Have a look at the link I've added to my answer :) – LihO Oct 17 '13 at 14:55
  • @LihO thank you that what i needed i.e to pass multiple arguments through pthead_create can you give link about pthreads and exercises for practice – Sri Harsha Oct 17 '13 at 16:30