Norms suggestion is both compiling and giving me results, the ** payload appears to be an issue for me at the moment, but I will be able to get that sorted out now.
the code Eli produced is so much nicer to look at and that might actually only need a tweak to get it working.
so currently Norms code works without modifications as an example of what my original question was aimed at.
I will update with anything more specific to the original question later on.
Are you sure? Norm’s pointer for the array in the middle of the struct made no sense to me. If it is, your data seems not compatible to what was said before.
yes, I want it to ‘destroy’ the structure elements by sharing its address with the pointer.
they basically have the same start address which is exactly what I was trying to get.
the pointer in the union is the same type as the array I am receiving, this appears in my compiler to cause no issue in operation of the code and I have the expected results.
I do not know if this code would be a danger in a PC app, but this is running in an 8 bit chip that has severe restrictions in memory management.
I have used a union in this way for years in lots of projects using this same compiler and it does what I was trying to do here, basically give the same address to the structure as the data array I create.
if it is unsafe I would appreciate a greater understanding of why.
if I have to change the existing code I would be happy to do so.
as it stands now, I recover all the data I expect to recover and, the pointer, however it is handled by this compiler, does not overwrite the data in the structure.
other uses I have is to efficiently split a signed 32 bit value into 4x8 bit values that are stored in a EEPROM which only has 8 bit elements.
union
{
int32 v32b;//the 32 bit value to be split up
uint8 b[4];//an array which will hold the individual data bytes
} sb;//the name of the union
sb.v32b = w_data;//load the 32 bit value into the union
‘w_data’ is a 32 bit signed value which places the 4 bytes in the array.
that code is 10 years in use at least, maybe I am relying on some quirk of this compiler for it to work?
I am thinking about why the code I am using works and I don’t know really.
as Rick suggests, the pointer SHOULD corrupt the data but it does not, I think this I some special way that the XC8 compiler handles pointers due to it being purely an 8 bit compiler.
You are not wrong, it’s not usual what you did instead of doing a cast of pointers, but my “destroying” argument was because, for one moment, I read the union as a struct and a pointer and not 2 pointers.