一道新人容易忽略的题目
- 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分别是多少,看到题目的时候不要机械的复制黏贴到编译器中
[解决办法]
不测试还真不知道~楼主解释下~
[解决办法]
???求解答!
[解决办法]
strlen()函数计算字符串长度,sizeof是返回变量所占内存数,故题目1 应该是 a为16,b为3.
[解决办法]
a=14
b=3
a=1
b=3
a=3
b=20
a=1
b=20
对吧?
[解决办法]
strlen
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
[解决办法]
[解决办法]
基础题 面试高频题 上次面试的这方面的题比楼主的题要难
[解决办法]
10L ++
[解决办法]
这个考的是'\0'的作用
[解决办法]
[解决办法]
对,正解就是这个
[解决办法]
\0 回复内容太短了!
[解决办法]
这是个考细节的,很有用