当前位置:网站首页>strlen跟sizeof区别
strlen跟sizeof区别
2022-07-30 12:51:00 【qq_25427995】
关于 strlen 与 sizeof 的区别:
strlen 是函数,sizeof 是运算符。
strlen 测量的是字符的实际长度,以’\0’ 结束(不包含’\0’ )。而sizeof 测量的是字符的分配大小,如果未分配大小,则遇到’\0’ 结束(包含’\0’ ,也就是strlen测量的长度加1),如果已经分配内存大小,返回的就是分配的内存大小。
比如:
char str[] = “Hello World”;
char str1[100] = “Hello World”;
cout << strlen(str) << endl;
cout << sizeof(str) << endl;
cout << sizeof(str1) << endl;
3.但是在子函数中,ziseof 会把从主函数中传进来的字符数组当作是指针来处理。指针的大小又是由机器来决定,而不是人为的来决定的。
#include
#include
#include
using namespace std;
void fun(char str[10])
{
cout << sizeof(str) << endl;
}
int main()
{
char str[] = “Hello World”;
fun(str);
return 0;
}
4.我们知道字符数组的初始化一次性赋值,也可以逐个赋值。可以指定数组的大小,也可以不指定数组长度。
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = “hello”;
char str2[] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};
char str3[] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’};
printf("str1:%s\n",str1);
printf("str2:%s\n",str2);
printf("str3:%s\n",str3);
printf("str1:%d\n", strlen(str1));
printf("str2:%d\n", strlen(str2));
printf("str3:%d\n", strlen(str3));
return 0;
}
这是为什么呢?
我们知道字符串是以 ‘\0’ 为结束标志的,所以char str1[ ] = “hello” 等效于char str2[ ] = {‘h’ , ‘e’ , ‘l’ , ‘l’ , ‘o’ , ‘\0’} 。strlen函数求的是字符串的实际长度,它求得方法是从开始到遇到第一个’\0’,如果你只定义没有给它赋初值,这个结果是不定的,它会从首地址一直找下去,直到遇到’\0’停止。而如果不在字符数组初始化的时候加上\0,那么strlen 得到的值就不是正确的数值,打印出来的结果也不是想要的结果。因此我们要避免这种情况,在初始化的时候要记得加上 \0,或者一次性赋初值。
5.strlen的结果要在运行的时候才能计算出来,是用来计算字符串的长度,不是类型占内存的大小。而大部分编译程序在编译的时候就把sizeof计算过了是类型或是变量的长度。
6.sizeof可以用类型做参数,strlen只能用char*做参数,且必须是以’‘\0’'结尾的。
原文链接:https://blog.csdn.net/zhengqijun_/article/details/51815081
边栏推荐
- EasyNVS云管理平台功能重构:支持新增用户、修改信息等
- matlab画图,仅显示部分图例
- 13-GuliMall Basics Summary
- 展厅全息投影所具备的三大应用特点
- 力扣——15. 三数之和
- datax enables hana support and dolphinscheduler enables datax tasks
- I built another wheel: GrpcGateway
- R语言筛选时间序列数据的子集(subset time series data)、使用window函数筛选连续日期时间范围内的数据(start参数和end参数分别指定起始和结束时间)
- 434. 字符串中的单词数
- There is no one of the strongest kings in the surveillance world!
猜你喜欢

Yilian: Activating the Value Potential of Data Elements and Unleashing the Innovation Dividend of SAS SSD

物理服务器与虚拟机:主要区别和相似之处

维护数千规模MySQL实例,数据库灾备体系构建指南

Apache Log4j2漏洞

ModelCoder状态机:对柴油机工况判断策略进行建模

PyQt5快速开发与实战 8.6 设置样式

MySQL【多表查询】

There is no one of the strongest kings in the surveillance world!

自从外包干了四年,基本废了...

MySQL查询性能优化
随机推荐
666666
Decoding Redis' most overlooked high CPU and memory usage issues
R语言时间序列数据算术运算:使用log函数将时间序列数据的数值对数化(平方、开平方、指数化等函数类似使用)
js 构造函数 return 非空对象,其实例化的对象在原型上的差异
grep时排除指定的文件和目录
RTSP/Onvif协议视频平台EasyNVR服务一键升级功能的使用教程
如何将EasyCVR平台RTSP接入的设备数据迁移到EasyNVR中?
物理服务器与虚拟机:主要区别和相似之处
leetcode207.课程表(判断有向图是否有环)
CMake library search function does not search LD_LIBRARY_PATH
EasyNVS云管理平台功能重构:支持新增用户、修改信息等
无人艇轨迹跟踪的预设性能抗扰控制研究
MySQL【多表查询】
[PostgreSQL] - explain SQL分析介绍
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
Analysis of AI recognition technology and application scenarios of TSINGSEE intelligent video analysis gateway
剑指 Offer 05. 替换空格
北上广线下活动丨年底最不可错过的技术聚会都齐了
EasyNVS cloud management platform function reconstruction: support for adding users, modifying information, etc.
dolphinscheduler adds hana support