• 关于sizeof 跟strlen 的区别


             char *t = "我a";

             char t1[MAX_PATH] = "aaaaaa";

            

             char display1[MAX_PATH];

             char display2[MAX_PATH];

             int len = sizeof(t);

             int len1 = strlen(t);

             itoa(len, display1, 10);

             itoa(len1, display2, 10);

             out(display1);

             out(display2);

    显示

    sizeof 在取指针char的时候,不管多长长度都是4

    sizeof 在取指针时,取到的是指针的长度,而不是char内容 的长度

    strlen 取到的则是char的长度,汉子占2个长度

    sizeof 在取char【maxpath】的时候,不管多长长度都是maxpath

    sizeof 在取指针时,取到的是数组定义的长度,而不是char内容 的长度

    strlen 取到的则是char的长度,汉子占2个长度

    streln 取得长度比sizeof更准确

    网上说法:

    #include "stdafx.h"
    #include "windows.h"
    #include <iostream>
    using namespace std;

    int _tmain(int argc, _TCHAR* argv[])
    {
     char str1[]="abcde";
     char str2[]="我是中国人";
     WCHAR str3[]=L"abcde";
     WCHAR str4[]=L"我是中国人";

     cout<<strlen(str1)<<endl;
     cout<<sizeof(str1)<<endl;
     cout<<endl;

     cout<<strlen(str2)<<endl;
     cout<<sizeof(str2)<<endl;
     cout<<endl;

     cout<<wcslen(str3)<<endl;
     cout<<sizeof(str3)<<endl;
     cout<<endl;

     cout<<wcslen(str4)<<endl;
     cout<<sizeof(str4)<<endl;
     cout<<endl; 

     return 0;
    }

    输出结果:

    5
    6

    10
    11

    5
    12

    5
    12

    请按任意键继续. . .

    由此可见,strlen返回的是字节数(对中英文不一致,中文占两个字节,不包括'/0'),而wcslen返回的是字符数(对中英文一致)。而sizeof返回的是字节数(包含'/0',而'/0'在Unicode下也是占两个字节的)。

    地址:http://blog.csdn.net/hczhiyue/article/details/6248229

  • 相关阅读:
    CSU1018: Avatar
    ZOJ
    HDU—4463 Outlets 最小生成树
    查询文件中值所在的路径
    mysql语句查询时间检测
    phpmyadmin修改root密码
    检测Linux glibc幽灵漏洞和修补漏洞
    监控宝安装手册
    ubuntu安装环境软件全文档
    ubuntu mysql主从库的搭建
  • 原文地址:https://www.cnblogs.com/youyaoqi/p/6197766.html
Copyright © 2020-2023  润新知