简体   繁体   中英

Casting cost addressing pointer in c from unsigned char * to const char *?

I am wondering how many there will be calculating at compile for casting of addressing.

eg

in strlen API defined :

size_t strlen ( const char * str );

they get arr only const char * .

but sometime I use like

unsigned char arr[] = "something"
strlen((const char *)arr);

just focus on unsigned char * to const char *

I wonder what cost there will be in code?

There is no runtime overhead associated with a cast in this way. It merely tells the compiler to access (or interpret) the variable differently from how it was declared.

In your specific case, the compiler is smart enough to generate code that treats a char array like a char* pointer with no overhead.

In general, casts in C never incur any runtime overhead.

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