学习ANSI C.(嵌入式所以malloc已经出局)。无法理解我如何使用不同缓冲区的N缓冲区(共享相同的结构),至少在没有编译器警告的情况下:
typedef struct Ringbuffer {
uint8_t wrIdx;
uint8_t lag;
//obvious, but with fixed size:
char buffer[32];
//what I'd like
uint8_t buffLength;
char *buffer;
} Ringbuffer;
//By concept...
Ringbuffer myRBs[3];
char rs485out[64];
char rs485in[16];
myRBs[0].buffLength=64;
myRBs[0].buffer = &rs485out;
myRBs[1].buffLength=16;
myRBs[1].buffer = &rs485in;
调试,代码似乎正常工作,但我收到GCC编译器警告:
# warning: assignment from incompatible pointer type at ( .. = &rs485out)
我在这做错了吗?警告是否可以修复?
答案 0 :(得分:6)
您不需要&
。当数组用作右值时,它将成为指向其第一个元素的指针。所以:
myRBs[0].buffer = rs485out;