I have a function for what I pass a matrix and a variable as a pointer (I simplified it, maybe in this form it doesn't make sense, but I didn't want to paste the whole code :)
uint64_t returndata = 0;
uint32_t binary_lock = 0;
void initLamp(uint8_t time, const uint8_t *datatable, uint64_t *outdata)
{
for(uint8_t k = 0; k < ROW; k++)
{
if(*(datatable + k * COLS) == time)
{
binary_lock |= (1 << k);
}
}
for(uint8_t i = 0; i < ROW; i++)
{
*outdata |= (1 << (3 * i));
}
}
I have another file, called datafield.h, what consists the matrix:
static const uint8_t signal_table[][COLS] =
{
{22, 24, 63, 67}, // 1A
{87, 89, 31, 35}, // 1B
{0 , 2, 21, 24}, // 1C
{0 , 3, 22, 43}, // 1D
}
In the main I need to refer to the function like this, to avoid errors and warnings:
int main(void)
{
initLamp(0, *signal_table, &returndata);
}
The question is: I'm a bit sceptic using the main function. I'm almost sure that the usage of '&returndata' is correct, but why do I have to put the '*' operator before the 'signal_table'. I thought I would only need to give the address of the matrix like &signal_table, but with this it is not working.
signal_tableis of typeconst uint8_t [4][4]. As an operand of unary*,signal_tableis converted fromconst uint8_t [4][4]toconst uint8_t (*)[4](pointer to array length 4 ofconst uint8_t), so*signal_tableis of typeconst uint8_t [4]. As a function call argument,*signal_tableis converted fromconst uint8_t [4]toconst uint8_t *, which is compatible with the function parameter. It would be more natural to replace*signal_tablewith&signal_table[0][0], which is also of typeconst uint8_t *and points to the same thing.char,signed char,unsigned char) which can legitimately be used to access any byte of the 2-D array. It is possible thatuint8_tis not defined as a character type on some C implementation (it could be defined as an extended integer type), which would disallow this usage. Ifuint8_tis the same type asunsigned charthen it is OK.