Search code examples
cmultidimensional-arrayavrimplicit-conversionfunction-declaration

Change array I pass to function without Return in C


This is my function:

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

And this is how I use it somewhere in my code:

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

And this is my array define:

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

but tx_buffer1 doesn't get values I give in data[]. I want to change tx_buffer1 but don't use return. Any help?


Solution

  • The array declared the following way

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

    used in the expression

    &tx_buffer1
    

    makes the expression type char ( * )[RX_BUFFER_SIZE1].

    At the same time the corresponding function parameter

    unsigned char *data[40]
    

    has the type unsigned char ** because the compiler implicitly adjusts a parameter having an array type to pointer to an object of the element type of the array.

    And moreover the function parameter uses the specifier unsigned char while the array declared with the specifier char.

    So the function call is invalid. There is no implicit conversion between the pointer types.

    There is no any sense to pass the array to a function by reference because in any case arrays are non-modifiable lvalues.

    If you want to pass the array by reference to know its size in the function then the function parameter shall be declared like

    char ( *data )[40]