主要指针能代替数组使用
https://blog.youkuaiyun.com/liusicheng2008_liu/article/details/80412586
char ** 与char * a[ ] ;
先看 char *a [ ] ;
由于[ ] 的优先级高于* 所以a先和 [ ]结合,他还是一个数组,数组中的元素才是char * ,前面讲到char * 是一个变量,保存的地址。。
所以 char *a[ ] = {"China","French","America","German"};
同过这句可以看到, 数组中的元素是字符串,那么sizeof(a) 是多少呢,有人会想到是五个单词的占内存中的全部字节数 6+7+8+7 = 28;
但是其实sizeof(a) = 16;
为什么,前面已经说到, 字符串常量的本质是地址,a 数组中的元素为char * 指针,指针变量占四个字节,那么四个元素就是16个字节了
char *uint8tob( uint8_t value ) {
static uint8_t base = 2;
static char buffer[8] = {0};
int i = 8;
for( ; i ; --i, value /= base ) {
buffer[i] = "01"[value % base];
}
return &buffer[i+1];
}
char *convert_bytes_to_binary_string( uint8_t *bytes, size_t count ) {
if ( count < 1 ) {
return NULL;
}
size_t buffer_size = 8 * count + 1;
char *buffer = calloc( 1, buffer_size );
if ( buffer == NULL ) {
return NULL;
}
char *output = buffer;
for ( int i = 0 ; i < count ; i++ ) {
memcpy( output, uint8tob( bytes[i] ), 8 );
output += 8;
}
return buffer;
};
int main(int argc, const char * argv[]) {
uint8_t bytes[4] = { 0b10000000, 0b11110000, 0b00001111, 0b11110001 };
char *string = convert_bytes_to_binary_string( bytes, 4 );
if ( string == NULL ) {
printf( "Ooops!\n" );
} else {
printf( "Result: %s\n", string );
free( string );
}
return 0;
}
... just extend for 16 bytes. There are many ways and it also depends on what do you mean with quick. Embedded systems, ...? You can make translation table to make it even faster, ...
UPDATE
char *convert_bytes_to_binary_string( uint8_t *bytes, size_t count ) {
if ( count < 1 ) {
return NULL;
}
const char *table[] = {
"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111"
};
size_t buffer_size = 8 * count + 1;
char *buffer = malloc( buffer_size );
if ( buffer == NULL ) {
return NULL;
}
char *output = buffer;
for ( int i = 0 ; i < count ; i++ ) {
memcpy( output, table[ bytes[i] >> 4 ], 4 );
output += 4;
memcpy( output, table[ bytes[i] & 0x0F ], 4 );
output += 4;
}
*output = 0;
return buffer;
};
int main(int argc, const char * argv[]) {
uint8_t bytes[4] = { 0b10000000, 0b11110000, 0b00001111, 0b11110001 };
char *string = convert_bytes_to_binary_string( bytes, 4 );
if ( string == NULL ) {
printf( "Ooops!\n" );
} else {
printf( "Result: %s\n", string );
free( string );
}
return 0;
}
sizeof(char**),sizeof(char*), sizeof(char)小注
sizeof(char) → 返回char型所占空间:1 (Byte)
sizeof(char*) → 返回char*型指针所占空间:4 (Byte)
sizeof(数组名) → 返回该字符串指针数组里元素所占空间:n*4(或8)(n为字符串指针数组元素个数,也即数组的字符串个数)
比如 char *strlist[] = {"American", "Germany", "Japan", "China", "France", "Russia"}
此时sizeof(strlist) = 6*4 = 24(Byte),代表着6个char*类型的总大小。
所以想求的字符串指针数组的字符串元素个数就可以用 sizeof(strlist)/sizeof(char*)来得到。
测试
char *strlist[] = {"American", "Germany", "Japan", "China", "France", "Russia"};
printf("sizof(int):%d\n",sizeof(int));
printf("sizof(char *):%d\n",sizeof(char *));
printf("sizof(char ):%d\n",sizeof(char ));
printf("sizeofstrlist%d, strlenstrlist:%d\n",sizeof(strlist),strlen(strlist));
printf("%s\n",strlist);
结果
sizof(int):4
sizof(char *):8
sizof(char ):1
sizeofstrlist48, strlenstrlist:3
q@@
不能用strlen 来计算 字符指针数组???