Reputation: 105
I have a 3-byte variable, but since there are no 3byte variables in C , I use a long 32bit instead. Then I want to send ONLY 3 bytes on the bus. I access the bus as ext Ram 8-bit width. To send only 3 bytes, i need to break the long value into 2-byte(a short) and 1-byte ( a char).
what I do is:
typedef union
{
unsigned char b[3];
unsigned long lng;
} lng_array;
void SendLong(unsigned long d)
{
volatile void* c =(void*)Dat; // Dat is a #defined long number equal to the address on the bus that data must be sent
lng_array tmp;
tmp.lng=d;
*c=*(unsigned short*)(&tmp.b[0]); // Error
*c=*(unsigned char*)(&tmp.b[2]); // Error
}
for the 2 "*c=" lines I get an error :"Error 36 invalid use of void expression "
What am I doing wrong here?
Upvotes: 0
Views: 142
Reputation: 28180
Since the variable c
is of type pointer to void, you cannot dereference it (e.g. reading/writing *c
). If you want to access the memory it points to you must add a type cast.
The C standard states that
6.3.2.2 void
The (nonexistent) value of a void expression (an expression that has type void) shall not be used in any way, ...
and since c
is of type pointer to void dereferencing it produces a void expression which then cannot be used for anything. Whatever there is on the other side of the equal sign is irrelevant with regards to this.
Upvotes: 2
Reputation: 35408
*c
makes no sense (cannot be derefenced) if the c
is a void*
since the compiler would need to know the size of it. Cast it again to a unsigned char*
, like: *((unsigned char*)(c)) = (unsigned char*)(&tmp.b[2]);
Upvotes: 3