关于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
关于sizeof 跟strlen 的区别的更多相关文章
- sizeof和strlen的区别
一.sizeof sizeof(...)是运算符,而不是一个函数. sizeof操作符的结果类型是size_t,在头文件中typedef为unsigned int,其值在编译时即计算好了, ...
- Sizeof与Strlen的区别与联系
转自:http://www.cnblogs.com/carekee/articles/1630789.html 一.sizeof sizeof(...)是运算符,在头文件中typedef为uns ...
- Sizeof与Strlen的区别与联系(转)
Sizeof与Strlen的区别与联系 一.sizeof sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型 ...
- C++-sizeof和strlen的区别
一.sizeof sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型.对象.函数等. 它的功能是:获得保 ...
- sizeof和strlen的区别和联系总结
link:http://blog.csdn.net/ghevinn/article/details/9974967 strlen所作的仅仅是一个计数器的工作,它从内存的某个位置(可以是字符串开头 ...
- 【转】Sizeof与Strlen的区别与联系
原文地址:http://www.cnblogs.com/carekee/articles/1630789.html 1.sizeof sizeof(...)是运算符,在头文件中typedef为uns ...
- C++Sizeof与Strlen的区别与联系
一.sizeof sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型.对象.函数等. 它的功能是:获得保 ...
- Sizeof与Strlen的区别【转】
本文转载自:http://www.cnblogs.com/carekee/articles/1630789.html Sizeof与Strlen的区别与联系 一.sizeof sizeof(.. ...
- 我也介绍下sizeof与strlen的区别
本节我也介绍下sizeof与strlen的区别,很简单,就几条: 1. sizeof是C++中的一个关键字,而strlen是C语言中的一个函数:2. sizeof求的是系统分配的内存总量,而strle ...
- C++基础--sizeof和strlen的区别
首先,来运行一段程序: #include "stdafx.h" #include <stdio.h> #include <string.h> int mai ...
随机推荐
- Spring MVC实现Junit Case
Spring MVC中编写单元测试(WEB项目): 1. 首先开发一个基类,用于载入配置文件.以下所有的测试实现类都要继承这个类 package com.yusj.basecase; import o ...
- 获取JAVA[WEB]项目相关路径的几种方法
在jsp和class文件中调用的相对路径不同. 在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getPro ...
- super getClass()
首先看一段代码: import java.util.Date;public class Test extends Date{ public static void main(String[] args ...
- 用delphi实现rar文件的解压的方法
http://blog.csdn.net/ad_home/article/details/2818936 准备工作 下载winrar提供的动态链接库unrar.dll http://w ...
- install cpanm
wget http://cpanmin.us mv index.html cpanm chmod +x cpanm
- SharePoint 2013 SP1发布了
好消息,SharePoint 2013 SP1终于发布了: SP1说明:http://support.microsoft.com/kb/2880552 下载页面:http://www.microsof ...
- Excel小写金额转大写金额公式
=IF(ROUND(A6,2)<0,"无效数值",IF(ROUND(A6,2)=0,"零",IF(ROUND(A6,2)<1,"" ...
- C++快速入门系列教程
C++ 60分钟入门教程:http://see.xidian.edu.cn/cpp/biancheng/view/2755.html
- NET映射导致的应用无法访问
应用环境 IIS 数据库 oracle11G 服务器 windwos2003 企业版 周一早上刚到,听到业务部门的人反应,集团的扫码抢系统登陆不上了,没办法进行出入库操作,大批货物在仓库堆积,承运车 ...
- IIS启用兼容模式设置(win2k3—Win7)
点击添加按钮(上图),弹出下面的对话框(下图).在自定义HTTP头名处输入: X-UA-compatible 在自定义HTTP头值处输入: IE=EmulateIE7 (输入时注意不要留下空格)输入完 ...