uint8_t Array Conversion to const char* in C

Viewed 37

Can you please help me convert this uint8_t array to a const char* in C?

uint8_t array = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, }

I am really having trouble passing it to a function which should be receiving a const char*.

1 Answers

For starters there are typos in this record

uint8_t array = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, }

You have to write at least like

uint8_t array[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, };

As for your question then just call the function like

func( ( const char * )array );

using explicit casting.

Here is a demonstration program.

#include <stdio.h>
#include <stdint.h>

void func( const char *s )
{
    for ( size_t i = 0; i < 6; i++ )
    {
        printf( "%d ", s[i] );
    }
    putchar( '\n' );
}

int main( void ) 
{
    uint8_t array[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, };
    func( ( const char * )array );
}

The program output is

0 1 2 3 4 5 
Related