0

I have a situation where in the address inside the void pointer to be copied to a another pointer. Doing this without a type cast gives no warnings or errors. The pseudo code looks like

structA A;
void * p = &A;
structA * B = p;// Would like to conform this step 

I don't foresee any problems with this. But since this operation is used over a lot of places, I would like to conform whether it can have any replications. Also is there any compiler dependency?

Ginu Jacob
  • 1,588
  • 2
  • 19
  • 35
  • 1
    See http://stackoverflow.com/questions/15745030/type-punning-with-void-without-breaking-the-strict-aliasing-rule-in-c99 – Pavel Šimerda Apr 02 '14 at 08:54
  • "Conform whether it has replications"? Why not use simple English: "I would like to check whether it works." – Kerrek SB Apr 02 '14 at 08:58
  • I had tested this with some pseudo code and it works ok. Still the question was intended to conform there is no possibility of any hidden issues.....Sorry if that had cause a confusion – Ginu Jacob Apr 02 '14 at 09:05

2 Answers2

5

No, this is fine and 100% standard.

A void * can be converted to and from any other data/object pointer without problems.

Note that data/object restriction: function pointers do not convert to/from void *.

This is the reason why you shouldn't cast the return value of malloc(), which returns a void *.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
1

In C a void * is implicitly compatible with any pointer. That why you don't need to cast when e.g. passing pointers of any type to functions taking void * arguments, or when assigning to pointer (still of any type) from function returning void * (here malloc is a good example).

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621