免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 3491 | 回复: 34
打印 上一主题 下一主题

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

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-06-01 09:24 |只看该作者 |倒序浏览
下面的程序:

  1. #inlcude "string.h"

  2. void main(void)
  3. {
  4.           unsigned char    j;                 // 1
  5.           unsigned char    s1[10];        // 2

  6.           j = sizeof(&s1);                      // 3
  7.           memset(&s1,0x10,sizeof(&s1));  // 4
  8.           while(1){}                                   // 5
  9. }
复制代码


这段程序单步跟踪运行到第四行时,j的值是对的,但运行到第五行时,j的值变为0x0100,如果把j设为全局变量,就没这问题,请问这是为什么?谢谢!

论坛徽章:
0
2 [报告]
发表于 2003-06-01 09:57 |只看该作者

请问sizeof 与strlen的区别?

我要是你,就这么写:

  1. #inlcude "string.h"

  2. void main(void)
  3. {
  4.           unsigned char    j;                 // 1
  5.           unsigned char    s1[10];        // 2

  6.           j = sizeof(s1);                      // 3
  7.           memset(s1,0x10,j);  // 4
  8.           while(1){}                                   // 5
  9. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2003-06-01 10:26 |只看该作者

请问sizeof 与strlen的区别?

[quote]原帖由 "JohnBull"][/quote 发表:
     

老大,两种方法结果都一样啊!

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
4 [报告]
发表于 2003-06-01 11:13 |只看该作者

请问sizeof 与strlen的区别?

楼主的sizeof用法错误

应该是sizeof(s1),而不是sizeof(&s1)

论坛徽章:
0
5 [报告]
发表于 2003-06-01 11:53 |只看该作者

请问sizeof 与strlen的区别?

原帖由 "gadfly" 发表:
楼主的sizeof用法错误

应该是sizeof(s1),而不是sizeof(&s1)
   


啊?在我的编译器里sizeof(&s1)与sizeof(s1)的结果都一样的。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
6 [报告]
发表于 2003-06-01 12:21 |只看该作者

请问sizeof 与strlen的区别?

你写这段程序的意图是什么呢?想验证什么?

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
7 [报告]
发表于 2003-06-01 12:27 |只看该作者

请问sizeof 与strlen的区别?

??一样?

是多少?

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

请问sizeof 与strlen的区别?

[quote]原帖由 "lenovo"]你写这段程序的意图是什么呢?想验证什么?[/quote 发表:
   

就是局部变量j的值在退出main()函数之前为什么会变化?
而j若是全局变量就没这问题了。难道大伙没看出这有问题吗?

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

请问sizeof 与strlen的区别?

原帖由 "gadfly" 发表:
??一样?

是多少?
   

单步跟踪运行到第4步时j的值为10,而到第5步时为0x0100,为什么?

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

请问sizeof 与strlen的区别?

大家帮我看看,下面的两段程序得出的 a,aa,my_temp_web的结果不同,请问这是为什么?谢谢!
  1. #include "string.h"

  2. #define u_char    unsigned char

  3. u_char code MY_WEB[] = {
  4. "<html>;\r\n"
  5. "</html>;\r\n"
  6. };

  7. const unsigned char REPLY_GET[] =              // 1st thing our server sends to a client
  8. {
  9.   "HTTP/1.0\r\n"                          // protocol ver 1.0, code 200, reason OK
  10.   "\r\n"                                         // indicate end of HTTP-header
  11. };


  12. void main(void)
  13. {
  14.         u_char my_temp_web[150];
  15.         u_char i;
  16.         u_char a,aa;
  17.        
  18.         a = strlen(MY_WEB) + strlen(REPLY_GET);
  19.         memcpy(my_temp_web,REPLY_GET,strlen(REPLY_GET));
  20.         memcpy(&my_temp_web[strlen(REPLY_GET)],MY_WEB,strlen(MY_WEB));
  21.         aa = strlen(my_temp_web);       

  22.         while(1){i = 1;}
  23.        
  24. }
复制代码

  1. #include "string.h"

  2. #define u_char    unsigned char

  3. u_char code MY_WEB[] = {
  4. "<html>;\r\n"
  5. "</html>;\r\n"
  6. };

  7. const unsigned char REPLY_GET[] =              // 1st thing our server sends to a client
  8. {
  9.   "HTTP/1.0\r\n"                          // protocol ver 1.0, code 200, reason OK
  10.   "\r\n"                                         // indicate end of HTTP-header
  11. };


  12. void main(void)
  13. {
  14.         u_char my_temp_web[150];
  15.         u_char i;
  16.         u_char a,aa;
  17.        
  18.         a = sizeof(MY_WEB) + sizeof(REPLY_GET);
  19.         memcpy(my_temp_web,REPLY_GET,sizeof(REPLY_GET));
  20.         memcpy(&my_temp_web[sizeof(REPLY_GET)],MY_WEB,sizeof(MY_WEB));
  21.         aa = sizeof(my_temp_web);       

  22.         while(1){i = 1;}
  23.        
  24. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP