c++ - How do I cast a void pointer to a int[3]? -
i need call 3rd party library , pass in int[3] void * [works]:
int pattern[3] = {2,4,10}; if ( ostaskcreate( blinkled, ( void * ) pattern, ( void * ) &blinktaskstack[user_task_stk_size], ( void * ) blinktaskstack, main_prio - 1 ) != os_no_err ) { iprintf( "*** error creating blink task\r\n" ); }
but need parse string pattern array , can't seem right.
first pass string parser , array:
int (&parseblinkoncommand(char rxbuffer[3]))[3] { // code parses rxbuffer , creates 3 ints needed int pattern[3] = {repeats, onticks, offticks}; return pattern; }
then try pass ostaskcreate did before:
int pattern2[3] = parseblinkoncommand(rxbuffer); if ( ostaskcreate( blinkled, ( void * ) pattern2, ( void * ) &blinktaskstack[user_task_stk_size], ( void * ) blinktaskstack, main_prio - 1 ) != os_no_err ) { iprintf( "*** error creating remote blink task\r\n" ); }
but error 'array must initialized brace-enclosed initializer'.
what right way this?
first, parseblinkoncommand
returns reference local object , return dangling reference.
second c-array not copyable, int pattern2[3] = parseblinkoncommand(rxbuffer);
should int (&pattern2)[3] = parseblinkoncommand(rxbuffer);
.
but why not using std::vector
or std::array
(or custom structure) ?
std::vector<int> parseblinkoncommand(const char (&rxbuffer)[3]) { // code parses rxbuffer , creates 3 ints needed return {repeats, onticks, offticks}; }
and then
auto pattern2 = parseblinkoncommand(rxbuffer); if ( ostaskcreate( blinkled, pattern2.data(), &blinktaskstack[user_task_stk_size], blinktaskstack, main_prio - 1 ) != os_no_err ) { iprintf( "*** error creating remote blink task\r\n" ); }
Comments
Post a Comment