• sizeof()和 strlen()的区别 --- 个人笔记


      在学习C语言和linux的时候,遇到了一些常见问题、题目,有些很简单,有些容易出错,本人水平有限,未免会出错,今天有时间,就将以前做的笔记,一一拿出来,写写blog。

    sizeof()和 strlen()的区别

      首先sizeof()是C语言提供的一个运算符,不是函数!它的作用主要是获得一个类型
    或者一个对象里面内容的空间大小。

      strlen()是一个标准库函数函数,用于计算一个字符串的长度,但是不把字符 ''计算在内。

    如char *buf="hello,world"

    strlen(buf)=11

    sizeof(buf)=4

    看看:sizeof( 2 + 3.14 );// 3.14的类型为double,2也会被提升成double类型,所以等价于 sizeof( double );

    再看看下面:

    static inline void print_size(void)
    {
          char str[] = "hello";
          char *pt = str ;
          char *ptr= NULL;
          void *p = malloc(100);
          int i =10;
          printf("sizeof str is %d
    strlen of str is %d
    ",sizeof(str),strlen(str));//6,5
          printf("sizeof pt is %d
    strlen of pt is %d
    ",sizeof(pt),strlen(pt));//4,5
          printf("sizeof ptr is %d
    ",sizeof(ptr));//4
          printf("sizeof p is %d
    ",sizeof(p));//4
          printf("sizeof i is %d
    ",sizeof(i));//4
    return ; }
  • 相关阅读:
    vi编辑器
    在shell脚本中使用函数
    在shell脚本中进行条件控制以及使用循环
    shell指令expr和test指令
    利用ps指令查看某个程序的进程状态
    shell变量的使用
    创建和运行shell脚本程序
    关于强制类型转换(c语言)
    elastic 常用查询操作
    elastic 集群安装
  • 原文地址:https://www.cnblogs.com/chengliangsheng/p/3600908.html
Copyright © 2020-2023  润新知