0
votes

I have a function which is defined:

uint32_t match_data(uint8_t * data_in, uint16_t size_data_in);

where I am trying to use the following typedef struct as an argument

typedef struct
{
uint8_t chars[5]
uint8_t ADC;
}Data

Data input;

input.chars[0] = 65;
input.chars[1] = 66;
input.chars[2] = 67;
input.chars[3] = 68;
input.chars[4] = 69;
input.ADC = 255;

match_data((uint8_t *)input, sizeof(input)); 

match_data() function is returning Operand of type 'Data' where arithmetic or pointer type is required

How can I cast a typedef struct to uint8_t? if I use it as a reference & I get the same error

I can cast it directly if I use only the char array, but not when using a typedef struct

1
match_data((uint8_t *) &input, sizeof(Data)); - sturcotte06

1 Answers

1
votes

You need to cast address of the struct-object, not the struct-object itself:

match_data((uint8_t *)&input, sizeof(input));

This works since the address of the object is guaranteed to be equal to that of the first member (i.e. chars).

Note, however, that it is unsafe/undefined behaviour to access the other data members, i.e. ADC through such a pointer. This is because the compiler may introduce padding bytes between the members, and accessing such padding area is undefined behaviour (since padding areas are in indeterminate state; cf., for example, this online C standard draft):

J.2 Undefined behavior ...The value of an object with automatic storage duration is used while it is indeterminate.

So actually I'd say you should pass chars directly:

match_data(input.chars, sizeof(input.chars));