在 C 中更改数组我传递给没有 Return 的函数

Change array I pass to function without Return in C

这是我的功能:

void eeprom_read_page(unsigned int address, unsigned char lengh, unsigned char *data[40])
{
    //unsigned char data[lengh] , i;
    unsigned char  i;
    i2c_start();
    i2c_write(EEPROM_BUS_ADDRESS_W);
    i2c_write(address>>8);          //high byte address
    i2c_write(address*0xff);        //low byte address
    i2c_start();
    i2c_write(EEPROM_BUS_ADDRESS_R);
    for(i=0 ; i<(lengh-1) ; i++)
    {
       *data[i+4]=i2c_read(1);
    }
    *data[lengh+3]=i2c_read(0);
    i2c_stop();
}

这就是我在代码中某处使用它的方式:

eeprom_read_page(   ( (rx_buffer1[1]*256)+rx_buffer1[2] ) , rx_buffer1[3] , &tx_buffer1 );

这是我的数组定义:

#define RX_BUFFER_SIZE1 40
char rx_buffer1[RX_BUFFER_SIZE1],tx_buffer1[RX_BUFFER_SIZE1];

但是 tx_buffer1 没有得到我在 data[] 中给出的值。我想更改 tx_buffer1 但不使用 return。有帮助吗?

数组声明方式如下

#define RX_BUFFER_SIZE1 40
char rx_buffer1[RX_BUFFER_SIZE1],tx_buffer1[RX_BUFFER_SIZE1];

用于表达式

&tx_buffer1

使表达式类型为 char ( * )[RX_BUFFER_SIZE1]

同时对应函数参数

unsigned char *data[40]

具有类型unsigned char **,因为编译器将具有数组类型的参数隐式调整为指向数组元素类型的对象的指针。

此外,函数参数使用说明符 unsigned char,而数组声明时使用说明符 char。

所以函数调用无效。指针类型之间没有隐式转换。

通过引用将数组传递给函数没有任何意义,因为在任何情况下数组都是不可修改的左值。

如果您想通过引用传递数组以了解其在函数中的大小,则函数参数应声明为

char ( *data )[40]