jeudi 30 juin 2016

C: Passing 2-dimensional array to function and using it

In a C program (to be compiled by Visual Studio 2013) I need to pass a pointer to a 1-dimensional array as a parameter of a function. The function writes values into the array. In another function I need to pass a two-dimensional array. This time the function does not need to write into the array - it just uses the values in it. I would like the functions to be useable with arrays of different sizes (i.e. I don't want to have to specify the number of elements (or in the case of the 2D array to number of sub-arrays of two elements each) in the array. I suppose I need to pass pointers to the arrays as parameters, but I'm not sure of the syntax for this, or the circumstances in which the array identifier can be used as a pointer to the array, or how this works for a 2D array.

How do I a) Define the functions? b) Define the arrays that I am going to pass to the functions? c) Call the functions? d) Refer to elements of the arrays within the functions?

Everything I have tried so far has given errors of the form "'uchar **' differs in levels of indirection from 'uchar [1]'" or "different types for formal and actual parameter 3" or "'uchar ()[2]' differs in levels of indirection from 'uchar [16][2]'".

Here is some code:

int i2c_write(int device_addr, uchar *data[][], int bytes)
{
}

int i2c_read(int device_addr, int register_addr, uchar *data[], int bytes)
{
}

    uchar readbyte[1];
    uchar writedata[16][2];

    if (i2c_read(0x76, 0xD0, readbyte, 1))  
    { etc.

    writedata[0][0] = 0xE0;
    writedata[0][1] = 0xB6;
    if (i2c_write(0x76, writedata, 1))
    { etc.

Thanks - Rowan

Aucun commentaire:

Enregistrer un commentaire