运行正常:
byte (*bufferedPacket)[nbPrioLP][lePrioLP] = &outBuffPacketPrio;
但是我需要这样做:
byte **bufferedPacket = NULL; // wrong way
if ( _cmd == "prio" ) (*bufferedPacket)[nbPrioLP][lePrioLP] = &outBuffPacketPRIO;
else (*bufferedPacket)[nbLP][leLP] = &outBuffPacket;
这是在函数内部,我想用不同大小的数组初始化缓冲区。
如何初始化指针?
谢谢。
编辑
这段代码位于函数内部。这个函数正在管理数据包的缓冲区。我有两种类型的数据包:8和30字节长。为了优化内存,数据包的数量不同(10和19个数据包)。
两个缓冲区(数组outBuffPacketPRIO, outBuffPacket)是来自主循环的全局变量。该函数有静态变量来记住数据包的位置,以清空/填充缓冲区。
在函数内部,我尝试指向2个缓冲区,以避免编写两个相同的函数,只有一个不同的缓冲区(outBuffPacketPRIO, outBuffPacket)。
我这样调用函数:
doByteBuffer("regular", 229, "add"); // add byte in the buffer
编辑2
我有这个函数
byte doByteBuffer(String _prio, byte _byte, String _action) {
static byte posAddBytePrio, posAddPacketPrio, posTakePacketPrio, posAddByteReg, posAddPacketReg, posTakePacketReg;
static unsigned long timePrio, timeReg;
if ( _prio == "prio" ) return doBuffer( _action, _prio, &(outBuffPacketPrio[nbPrioLP][0]), &timePrio, &posTakePacketPrio, nbPrioLP, &posAddPacketPrio, lePrioLP, &posAddBytePrio, _byte );
if ( _prio == "regular" ) return doBuffer( _action, _prio, &(outBuffPacket[nbLP][0]), &timeReg, &posTakePacketReg, nbLP, &posAddPacketReg, leLP, &posAddByteReg, _byte );
}
调用这个:
byte doBuffer(String _action, String _prio, byte *_buffer, unsigned long *_time, byte *_posTakePacket, byte _nb, byte *_posAddPacket, byte _len, byte *_posAddByte, byte _byte) {
boolean cond1, cond2, cond3;
// init
if ( *_posAddPacket >= _nb ) *_posAddPacket = 0; // cycle counting
if ( *_posTakePacket >= _nb ) *_posTakePacket = 0; // cycle counting
// packet overflow
if ( (_action == "add") || (_action == "end") || (_action == "ack") ) {
cond1 = ( *_posTakePacket == *_posAddPacket );
if ( (*_posTakePacket+1) >= _nb ) cond2 = ( *_buffer[0][0] != 0 ); // WRONG WAY
...
}
我在这个函数的最后一行得到这个错误:
SCD_XBee.ino: In function 'byte doBuffer(String, String, byte*, long unsigned int*, byte*, byte, byte*, byte, byte*, byte)':
SCD_XBee:25: error: invalid types 'unsigned char[int]' for array subscript
你知道我做错了什么吗?
这是否意味着bufferedPacket是一个指向二维字节数组的指针?你只是把它分配给一个已经分配了正确大小的对象?那么,为什么需要一个函数呢?
好,你把字节放入一个或另一个数组。你只需要:
if ( strncmp( _cmd, "prio", 4 ) )
{
doAddByte( &(outBuffPacketPRIO[nbPrioLP][0]), lePrioLP, _byte );
nbPrioLP++;
}
else
{
doAddByte( &(outBuffPacket[nbLP][0]), leLP, _byte );
nbLP++;
}
void doAddByte( byte* buffer, int buflen, byte _b )
{
// buffer is now the pointer to the packet you want to add
// and buflen is the max size of the packet
buffer[3] = _b;
}
谢谢你JonS,你给了我这个主意。然后我看到这个:"有时你需要写一个函数来接受一个二维数组,其中宽度直到运行时才知道。在这种情况下,您可以传递一个指向[0][0]元素的指针,同时传递两个维度,然后使用指针算术来获得正确的元素,例如:x[i * width + j]"
我是这样解决的:
byte doByteBuffer(String _prio, byte _byte, String _action) {
static byte posAddBytePrio, posAddPacketPrio, posTakePacketPrio, posAddByteReg, posAddPacketReg, posTakePacketReg;
static unsigned long timePrio, timeReg;
if ( _prio == "prio" ) return doBuffer( _action, _prio, &outBuffPacketPrio[0][0], &timePrio, &posTakePacketPrio, nbPrioLP, &posAddPacketPrio, lePrioLP, &posAddBytePrio, _byte );
if ( _prio == "regular" ) return doBuffer( _action, _prio, &outBuffPacket[0][0], &timeReg, &posTakePacketReg, nbLP, &posAddPacketReg, leLP, &posAddByteReg, _byte );
}
byte doBuffer(String _action, String _prio, byte *_buffer, unsigned long *_time, byte *_posTakePacket, byte _nb, byte *_posAddPacket, byte _len, byte *_posAddByte, byte _byte) {
boolean cond1, cond2, cond3;
// init
if ( *_posAddPacket >= _nb ) *_posAddPacket = 0; // cycle counting
if ( *_posTakePacket >= _nb ) *_posTakePacket = 0; // cycle counting
// packet overflow
if ( (_action == "add") || (_action == "end") || (_action == "ack") ) {
cond1 = ( *_posTakePacket == *_posAddPacket );
if ( ( (*_posTakePacket) + 1 ) >= _nb ) cond2 = ( _buffer[0 * _len + 0] != 0 ); // _buffer[0][0]
else cond2 = ( _buffer[ ((*_posTakePacket)+1)*_len + 0 ] != 0 ); // _buffer[posTakePacket+1][0]
if ( cond1 && cond2 ) {
....