Wednesday 15 July 2015

c - Pass uint16_t array into function that takes uint8_t* -



c - Pass uint16_t array into function that takes uint8_t* -

in c, i'd pass uint16_t array function takes uint8_t:

bool fctn(uint8_t *data, size_t length);

i have array, within struct:

typedef struct sstruct sstruct; struct sstruct{ uint16_t d[10]; } sstruct mystruct;

so do:

void test(sstruct structdata) { fctn( (uint8_t*)structdata->d[0], 10*sizeof(uint16_t) ); }

the length isn't issue here, it's passing array. if array weren't pointer i'm getting "error: cast pointer integer of different size", how can create array of uint8_t? far function concerned, uint16_t array double length array of size uint8_t, i'm not sure how tell compiler this...when input pointer.

if array 'd' wasn't within struct, global, i'd do

fctn( (uint8_t*)d, 10*sizeof(uint16_t) );

what's right way function pass array within passed struct?

when uint16_t d[10];, defining array d. when d[0], access first element of array, of type uint16_t. function expecting pointer, should cast pointer array, not element in it:

fctn( (uint8_t*)d, 10*sizeof(uint16_t) );

when this, d decayed type uint16_t[10] uint16_t*, cast uint8_t* before beingness passed fctn.

as aside, can utilize sizeof(d) simplify length calculation:

fctn( (uint8_t*)d, sizeof(d) );

note works if d declared array (on stack).

c arrays uint8t

No comments:

Post a Comment