gpt4 book ai didi

更改我传递给函数而不用 C 中的 Return 的数组

转载 作者:行者123 更新时间:2023-11-30 14:31:45 26 4
gpt4 key购买 nike

这是我的功能:

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]

关于更改我传递给函数而不用 C 中的 Return 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60136990/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com