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

关于sizeof 跟strlen 的区别的更多相关文章

  1. sizeof和strlen的区别

    一.sizeof    sizeof(...)是运算符,而不是一个函数.    sizeof操作符的结果类型是size_t,在头文件中typedef为unsigned int,其值在编译时即计算好了, ...

  2. Sizeof与Strlen的区别与联系

    转自:http://www.cnblogs.com/carekee/articles/1630789.html 一.sizeof    sizeof(...)是运算符,在头文件中typedef为uns ...

  3. Sizeof与Strlen的区别与联系(转)

    Sizeof与Strlen的区别与联系 一.sizeof     sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型 ...

  4. C++-sizeof和strlen的区别

    一.sizeof    sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型.对象.函数等.    它的功能是:获得保 ...

  5. sizeof和strlen的区别和联系总结

    link:http://blog.csdn.net/ghevinn/article/details/9974967    strlen所作的仅仅是一个计数器的工作,它从内存的某个位置(可以是字符串开头 ...

  6. 【转】Sizeof与Strlen的区别与联系

    原文地址:http://www.cnblogs.com/carekee/articles/1630789.html 1.sizeof  sizeof(...)是运算符,在头文件中typedef为uns ...

  7. C++Sizeof与Strlen的区别与联系

    一.sizeof    sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型.对象.函数等.    它的功能是:获得保 ...

  8. Sizeof与Strlen的区别【转】

    本文转载自:http://www.cnblogs.com/carekee/articles/1630789.html Sizeof与Strlen的区别与联系 一.sizeof    sizeof(.. ...

  9. 我也介绍下sizeof与strlen的区别

    本节我也介绍下sizeof与strlen的区别,很简单,就几条: 1. sizeof是C++中的一个关键字,而strlen是C语言中的一个函数:2. sizeof求的是系统分配的内存总量,而strle ...

  10. C++基础--sizeof和strlen的区别

    首先,来运行一段程序: #include "stdafx.h" #include <stdio.h> #include <string.h> int mai ...

随机推荐

  1. 树莓派开启root用户

    树莓派系统使用的linux是debian系统,所以树莓派启用root和debian是相同的. debian里root账户默认没有密码,但账户锁定. 当需要root权限时,由默认账户经由sudo执行,R ...

  2. AttributeError: type object '_io.StringIO' has no attribute 'StringIO'

    python2导入StringIO模块,直接: from StringIO import StringIO 对于python3,StringIO和cStringIO模块已经没了,如果要使用的话,需要导 ...

  3. DIJ产品系列

  4. JavaScript的学习3

    一.数组 1.定义数组格式:var 变量名 = [数组元素1,数组元素2] 2.遍历数组元素: 格式: var arr = []; for(var i=0;i<数组长度;i++){ arr[i] ...

  5. 测不准的程序员(Heisenberg Developers)

    摘译自http://mikehadlow.blogspot.co.uk/2014/06/heisenberg-developers.html   ——“你无法在不改变他们状态的情况下观察一个开发者” ...

  6. 简约的ASP.NET 系统框架,提供源码。

    好东西,好东西就要分享. 其实谈不上什么好东西,但很实用,应用起来也很简单,我自己用它构建了多套ASP .NET系统,原理也很简单: 1.用XML文件编辑系统的菜单和Toolbar: 2.读取XML节 ...

  7. Raspberry pi之wifi设置-3

    1.配件套装里最好有wifi网卡,非常小如下图 插入Raspberry pi的USB口,用lsusb来查看USB设备列表如下 pi@raspberrypi~/Desktop $ lsusb Bus 0 ...

  8. .net 制作二维码

    首先  要下载  ThoughtWorks.QRCode.dll 下载地址:http://download.csdn.net/detail/lybwwp/6861821 然后新建项目 把Thought ...

  9. 《从零开始做一个MEAN全栈项目》(4)

    欢迎关注本人的微信公众号"前端小填填",专注前端技术的基础和项目开发的学习. 在上一篇中,我们讲了如何去构建第一个Express项目,总结起来就是使用两个核心工具,express和 ...

  10. Android 自定义表格显示数据

    Android 自定义TextView控件,用来组成表格方便数据的展示. 首先看一下效果 样式不是很好看,需要用的可以自己优化一下. 实现方式很简单. 1.自定义控件 MyTableTextView ...