char a[]={'f','f','f','f'};strlen(a),sizeof(a)等于多少 c语言中sizeof()和strlen()用法上有什么区别呢...

\u6709\u4ee5\u4e0b\u7a0b\u5e8f#include <stdio.h>main(){ char a[10]="abcd";printf("%d,%d\n",strlen(a),sizeof(a));

\u8f93\u51fa4,10\uff1b
\u5b57\u7b26\u4e32a\u7684\u957f\u5ea6\u662f4\uff0c\u5b57\u7b26\u4e32a\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u662f10\u4e2a\u5b57\u7b26\uff0c\u4e5f\u5c31\u662f10\u4e2a\u5b57\u8282\u3002

sizeof\u662f\u64cd\u4f5c\u7b26\uff0c\u4e0d\u662f\u51fd\u6570\u3002\u5b83\u540e\u9762\u7684()\u5e76\u4e0d\u662f\u5fc5\u987b\u7684\uff0c\u6ca1\u6709\u6b67\u610f\u65f6\u5c31\u4e0d\u5fc5\u5199\u4e0a\u4e86\u3002sizeof\u64cd\u4f5c\u7b26\u8ba1\u7b97\u7684\u662f\u7cfb\u7edf\u4e3a\u5b83\u540e\u9762\u7684\u53d8\u91cf\u6216\u7c7b\u578b\u540d\u5206\u914d\u7684\u7a7a\u95f4\u5927\u5c0f\uff0c\u65e0\u8bba\u5176\u4e2d\u662f\u5426\u5b58\u50a8\u4e86\u6709\u7528\u6570\u636e\u3002
strlen()\u662f\u5e93\u51fd\u6570\uff0c()\u4e2d\u5fc5\u987b\u662f\u5b57\u7b26\u4e32\u6307\u9488\u6216\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u5b83\u8fd4\u56de\u7684\u662f\u68c0\u6d4b\u5bf9\u8c61\u4e2d\u7b2c\u4e00\u4e2a'\0'\u524d\u7684\u5b57\u7b26\u4e2a\u6570\uff0c\u4e0d\u542b'\0'\u3002
\u4e0b\u9762\u7684\u4ee3\u7801\u53ef\u80fd\u5bf9\u6b64\u89e3\u60d1\uff1a
//#include "stdafx.h"//If the vc++6.0, with this line.#include "stdio.h"#include "string.h"int main(void){ char s[50]="1234",p[100]={'f','j','k','d','s','a','l','\0','1','2','3','4','5','\0'}; printf("sizeof(s) = %d\n",sizeof s);//\u8fd9\u91ccs\u6ca1\u6709\u52a0(),\u4e5f\u6b63\u786e printf("strlen(s) = %d\nstrlen(p) = %d\n",strlen(s),strlen(p)); //\u4e0a\u53e5\u68c0\u6d4bp\u65f6\u9047\u5230\u7b2c\u4e00\u4e2a'\0'\u5c31\u7ed3\u675f\u4e86 return 0;}

strlen(a)是测试数组里面的内容的长度。这里因为没有'\0'结尾,所以值是不正确的,我试了一下,strlen(a) = 7,显然不正确。
sizeof(a);是测试整个数组的内存长度。sizeof(a) = 4

strlen(a)不能确定,每次运行结果可能不一样,sizeof(a)=4

sizeof(a)为4;

strlen(a)的值不确定,从数组a的首地址开始,遇到'\0'之前,有多少个字符,就是多少。

扩展阅读:www.sony.com.cn ... paperpass免费入口 ... mac蜜桃奶茶314 ... e人e本a2 ... char str 10 china ... char str 1234 ... www.vivo.com ... 求解方程计算器 ... char a='\72' ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网