首先,来运行一段程序:

#include "stdafx.h"
#include <stdio.h>
#include <string.h> int main()
{
char *a = "aaaa";
int x = sizeof(a);
int y = strlen(a); printf("%d\n", x);
printf("%d\n", y); return 0;
}

运行结果为:

再看下面这段程序:

#include "stdafx.h"
#include <stdio.h>
#include <string.h> int main()
{
char *a = "aaa";
int x = sizeof(a);
int y = strlen(a);
int s = sizeof(char *);
int s1 = sizeof(char);
//int s2 = strlen(char*);
printf("%d\n", s);
printf("%d\n", s1);
printf("%d\n", x);
printf("%d\n", x); return ;
}

运行结果为:

以上结果表示char*占用的内存大小为4, char占用的内存大小为1;

运行以上两个程序,可以总结出sizeof和strlen的区别为:

1. sizeof的参数可以是数据类型,也可以是变量,而strlen只能以以'\0'结尾的字符串作为参数;

2. sizeof计算的是数据类型占内存的大小,strlen计算的是字符串实际的长度

3. sizeof是操作符,strlen是string的库函数;编译器是编译时就计算出了sizeof的结果,而strlen只有在运行时才计算;

4. sizeof的参数为原参数,strlen的参数会变化为指针

C++基础--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 ...

随机推荐

  1. MySQL数据查询结果导出生成文件

    select url from news where url like "%美女%"  into outfile  "/导出的文件路径" : 在这里有个坑,对于 ...

  2. http 与 TCP 与 socket 定义,区别

    http://blog.csdn.net/jenminzhang/article/details/47017741 http协议:处于互联网协议的应用层,角色是规定数据传输的格式,http是建立在so ...

  3. ubuntu 搭建django 环境

    ubuntu 默认安装了 python2.7 . 安装django apt install python-django: 安装mysql apt install mysql-server*  *代表版 ...

  4. C语言中的预处理命令

    预处理功能是C语言的重要功能. 问:为什么要预处理,什么是预处理? 答:我们知道高级语言的运行过程是通过编译程序(编译器)把源代码翻译成机器语言,实现运行的.编译程序的工作包含:语法分析.词法分析.代 ...

  5. Device eth0 does not seem to be present, delaying initialization: Linux Networking

    copy centos 报错 Device eth0 does not seem to be present, delaying initialization: Linux Networking # ...

  6. hive复杂格式array,map,struct使用

    -- 创建数据库表,以array作为数据类型 drop table if exists person; create table person( name string ,work_locations ...

  7. mgo03_linux7上安装mongo4.0

    下载地址https://www.mongodb.com/download-center#community tar -xvf mongodb-linux-x86_64-rhel70-4.0.0.tgz ...

  8. mysql查询过去12个月的数据统计

    SELECT DATE_FORMAT( FROM_UNIXTIME( t.`created_at`, '%Y-%m-%d %H:%i:%S' ), '%Y-%m' ) MONTH, count(t.c ...

  9. 虚拟机 --- windows传输文件到虚拟机内

    安装xftp 如果未安装的,可以点击上图红框的图标,会有链接....下载时记得选择school那一个身份,这是免费的...邮箱必须要写,因为下载链接会发送到你的邮箱里,如果没收到就换一个邮箱 下载完后 ...

  10. python面向对象, 单例模式

    目录 单利模式 实现单利模式的方法 使用模块 使用__new__ 为了使类只能出现一个实例,我们可以使用 new 来控制实例的创建过程,代码如下: 使用装饰器 使用 metaclass 补充:元类(m ...