初始化指向二维数组的指针,然后将其与另一个数组链接

时间:2014-03-12 21:01:19

标签: c arrays pointers multidimensional-array

这很好用:

byte (*bufferedPacket)[nbPrioLP][lePrioLP] = &outBuffPacketPrio;

但我需要做这样的事情:

byte **bufferedPacket = NULL; // wrong way

if ( _cmd == "prio" ) (*bufferedPacket)[nbPrioLP][lePrioLP] = &outBuffPacketPRIO; 
else (*bufferedPacket)[nbLP][leLP] = &outBuffPacket;

这是在函数内部,我想用不同大小的数组初始化缓冲区。

我应该如何初始化指针?

感谢。

修改

此代码位于函数内部。此功能正在管理数据包缓冲区。我有两种类型的数据包:8和30字节长。为了优化RAM,数据包的数量是不同的(10和19包)。

2个缓冲区(数组outBuffPacketPRIO,outBuffPacket)是来自主循环的全局变量。该函数具有静态变量来记住数据包的位置以清空/填充缓冲区。

在函数内部,我尝试指向2个缓冲区以避免编写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

你知道我做错了吗?

2 个答案:

答案 0 :(得分:0)

这是不是意味着bufferedPacket是指向2-d字节数组的指针?而且您只是将它分配给已经分配了正确大小的对象?那么,你为什么需要一个函数?

好的,所以你把字节放到一个或另一个数组中。你基本上只是这样做:

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;
}

答案 1 :(得分:0)

谢谢你 JonS ,你给了我这个主意。然后我看到了这个:"有时你需要编写一个函数来接受一个二维数组,其中宽度在运行时才知道。在这种情况下,您可以将指针传递给[0] [0]元素并传递两个维度,然后使用指针算法来获取正确的元素,例如:x [i * width + j]&#34 ;

我是这样解决的:

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 ) {
....