[SOLVED] (byte*)(void*)&value ???

HI,

I've come across the above code in the following snippet -

// Read an integer from EEPROM
int read_int(byte position) {
  int value = 0;
  byte* p = (byte*)(void*)&value;
  for (int i = 0; i < sizeof(value); i++)
      *p++ = EEPROM.read(position++);
  return value;
}

& it's got me a bit stumped, I think this snippet is based on the template code from

http://playground.arduino.cc/Code/EEPROMWriteAnything

If anyone could spare the time for a brief explantion, it'd be much appreciated.

I understand the concept of pointers & type casting, but the void pointer with casting has me confussed.

Regards

byte* p = (byte*)&value;

The "(void*)" is redundant.

& in this context is "address of" see the ref section.

Mark

Ok, thanks 2 u both,

The "(void*)" is redundant.

That simplifies things a bit!

I'll still put a few debug prints to make sure I fully understand it all.

Although I'm not sure why the void bit was put in there in the first place, must have been some reason??.

Regards

Although I'm not sure why the void bit was put in there in the first place, must have been some reason?

Lack of understanding on the part of the author?
Cut and paste laziness ?

Ahh.. that type of redundancy!!

Although i'm a bit disappointed..... I thought I might have touched on a new level of C/C++ programming Alchemy :roll_eyes:!

thanks

byte* p = (byte*)(void*)&value;

If you break it down, working from right to left, "value" is an "int", so the type of "&value" is "pointer to int", aka "int*".
So then they cast "int*" to "void*" (an anonymous, vanilla pointer) and then cast "void*" to "byte*".
There could have been any number of casts between "&value" and "(byte*)", but the outcome would have been the same.

There could have been any number of casts between "&value" and "(byte*)", but the outcome would have been the same.

IIRC, C++ has some casting rules that are more complicated than those of C, so that there are SOME pointers or circumstances where you could not cast to a byte pointer ((byte*)) without first doing the cast to the generic pointer ((void*))
I am not enough of a C++ person to understand the details.

With (byte*)&value, even with the strictest compiler options I could find

-O0 -fno-elide-constructors -pedantic-errors -ansi -Wextra -Wall -Winit-self -Wold-style-cast -Woverloaded-virtual -Wuninitialized -Wmissing-declarations -Winit-self -std=c++98

, the worst you get is a warning "use of old-style cast", which can be cured by a reinterpret cast.

byte* p = reinterpret_cast <byte*>(&value);

The double cast "(byte*)(void*)" simply gives you two old-style cast warnings,

Abstruse C and C++ problems are often answered at stackoverflow.com,
its usually worth a search there (though not it seems this time - I wondered if
there was a rationale for a two-step cast, but couldn't find anything).

Following on AWOL's comments, if you want some help with Purdum's RIght-Left Rule and how it can be used to parse complex data definitions, see:

http://jdurrett.ba.ttu.edu/3345/handouts/RL-rule.html

econjack:
Following on AWOL's comments, if you want some help with Purdum's RIght-Left Rule and how it can be used to parse complex data definitions, see:

http://jdurrett.ba.ttu.edu/3345/handouts/RL-rule.html

If found, substitute English keywork.

"keyword"?

@AWOL: I don't understand your last post.

What's a "key work"?
It's not a word I recognise.
It's a direct quote from the link.

Opps...didn't see that...

Thanks all for your replies.

So then they cast "int*" to "void*" (an anonymous, vanilla pointer) and then cast "void*" to "byte*".

{penny_drops]{lightbulb_turns_on]

Ah I get it thanks AWOL! kind of like a transitory placeholder or something to allow coversion of pointers/types.

Mark1
Ok, I'll keep stackoverflow in mind.

econjack
Thanks for the link, I probably should review a few or quite a few examples on pointers so I become more familar with them.