简体   繁体   中英

uint8_t Array Conversion to const char* in C

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*.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM