免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234
最近访问板块 发新帖
楼主: hjs6142
打印 上一主题 下一主题

请问sizeof 与strlen的区别? [复制链接]

论坛徽章:
0
31 [报告]
发表于 2003-06-03 20:08 |只看该作者

请问sizeof 与strlen的区别?

原帖由 "hjs6142" 发表:
<html>;\r\n"
"</html>;\r\n"
};

(1)sizeof(s1) = ?
(2)strlen(s1) = ?

谢谢!
   

You were right.
This kind of problems have not a certain answer since we use different compiler.
You defined a ONE dimension char array by splitting a long string into two parts. I'm not sure whether your syntax is legal on all compilers.
In gcc, at least, sizeof s1 is the memory size of s1[] stays (including \0)
strlen s1 is the string size of s1[] (excluding \0)

论坛徽章:
0
32 [报告]
发表于 2003-06-03 20:26 |只看该作者

请问sizeof 与strlen的区别?

原帖由 "JohnBull"]In gcc, at least, sizeof s1 is the memory size of s1[ 发表:
stays (including \0)
strlen s1 is the string size of s1[] (excluding \0)
   

JohnBull斑竹,是这样的:在我的编译器中,比如:char s1 = {“abcdefg"};
sizeof(s1) = 8,也就是说它把最后的”\0“也算进去了;
而strlen(s1) = 7,不计算”\0“的;
请问这怎么解释?

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
33 [报告]
发表于 2003-06-03 21:20 |只看该作者

请问sizeof 与strlen的区别?

呵呵!挺热闹!

好好学习 C 语言!

先学语法,再做什么 CGI 程序。
这里边有一个顺序问题。

论坛徽章:
0
34 [报告]
发表于 2003-06-03 22:37 |只看该作者

请问sizeof 与strlen的区别?

同意。

顾名思义, sizeof(a) 是取a的大小. a 可以是任何类型。 比如, char, int, structure, pointer, etc.

strlen(a) 是去字符串a的长度。 i.e., 从a[0]到'\0'

基本上, 两者无可比性。 当然, 两者对A的返回直在一些情况下是相同的。

[quote]原帖由 "JohnBull"]ding \0)[/quote 发表:

论坛徽章:
0
35 [报告]
发表于 2003-06-06 15:46 |只看该作者

请问sizeof 与strlen的区别?

这才是关键
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP