我有一个3字节的变量,但是由于C语言中没有3字节的变量,所以我使用一个32位的长变量。那么我只想在总线上发送3个字节。我以8位宽的扩展内存访问总线。
要只发送3个字节,我需要将长值分成2字节(短)和1字节(字符)。
我做的是:

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
}

对于2“*c=”行,我得到一个错误:“错误36无效使用void表达式”
我在这里做错什么了?

最佳答案

*c如果c是一个void*就没有意义(不能取消限制),因为编译器需要知道它的大小。再次将其转换为unsigned char*,例如:*((unsigned char*)(c)) = (unsigned char*)(&tmp.b[2]);

关于c - void指针:怎么了?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22783602/

10-12 22:28
查看更多