一道新人容易忽略的题目C/C++ code1、char test[] {0x01,0x02,0x03}int a strlen(test)int b sizeo
一道新人容易忽略的题目
C/C++ code
1、char test[] = {0x01,0x02,0x03};int a = strlen(test);int b = sizeof(test);2、char test[] = {0x01,0x00,0x03};int a = strlen(test);int b = sizeof(test);3、char test[20] = {0x01,0x02,0x03};int a = strlen(test);int b = sizeof(test);4、char test[20] = {0x01,0x00,0x03};int a = strlen(test);int b = sizeof(test);//以上题目中的a、b分别是多少,看到题目的时候不要机械的复制黏贴到编译器中
The length of a C string is determined by the terminating null-character: A C string is as long as the amount of characters between the beginning of the string and the terminating null character.
a的值怎么确定? [解决办法] 11,3 1,3 3,20 1,20 为什么呢高手解释下 [解决办法] 1、 char test[] = {0x01,0x02,0x03}; int a = strlen(test); // 未知,不知道null在哪里,看栈上的内容 int b = sizeof(test); // 3
2、 char test[] = {0x01,0x00,0x03}; int a = strlen(test); // 1 int b = sizeof(test); // 3
3、 char test[20] = {0x01,0x02,0x03}; int a = strlen(test); // 3,其余的被初始化成0,即null int b = sizeof(test); // 20
4、 char test[20] = {0x01,0x00,0x03}; int a = strlen(test); // 1 int b = sizeof(test); // 20