zoukankan      html  css  js  c++  java
  • 头文件<stddef.h>

    头文件<stddef.h>定义了各种变量类型和宏。这些定义中的大部分也出现在其它头文件中。

    1、类型:
    ptrdiff_t 是指两个指针相减的结果的有符号整数类型
    size_t 是sizeof操作符的结果的无符号整数类型
    wchar_t 是一个整值类型,它范围内的值可以表示最大扩展字符集中所有成员的不用编码值,而该字符集是由支持它的区域设置指定的。空字符的编码值应该为0
    2、宏:
    NULL 展开为实现定义的空指针常量

    offsetof 展开为一个size_t类型的整值常量表达式,它的值是从结构的起始位置到结构成员的偏移量,以字节为单位。

    offsetof(type, member-designator)

    参数:
    type -- 这是一个 class 类型,其中,member-designator 是一个有效的成员指示器。
    member-designator -- 这是一个 class 类型的成员指示器
    返回值:
    该宏返回类型为 size_t 的值,表示 type 中成员的偏移量。

    例:

    #include <assert.h>
    #include <limits.h>
    #include <stddef.h>
    #include <stdio.h>

    typedef struct{
    char f1;
    struct{
    float flt;
    }f2;
    int f3;
    }Str;

    static char *pc=NULL;
    static double *pd=NULL;
    static size_t offs[]={
    offsetof(Str,f1),
    offsetof(Str,f2),
    offsetof(Str,f3)
    };

    int main()
    {
    ptrdiff_t pd=&pc[INT_MAX]-&pc[0];
    wchar_t wc=L'Z';
    Str x={1,2,3};
    char *ps=(char *)&x;

    assert(sizeof(ptrdiff_t)==sizeof(size_t));
    assert(sizeof(size_t)==sizeof(sizeof(char)));
    assert(pd=&pc[INT_MAX]-&pc[0]);
    assert(wc==L'Z');
    assert(offs[0]<offs[1]);
    assert(offs[1]<offs[2]);
    assert(*(char *)(ps+offs[0])==1);
    assert(*(float *)(ps+offs[1])==2);
    assert(*(int *)(ps+offs[2])==3);
    printf("sizeof (size_t) = %u ",sizeof(size_t));
    printf("sizeof (wchar_t) = %u ",sizeof(wchar_t));
    puts("success testing <stddef.h>");
    return (0);
    }

    结果:

    sizeof (size_t) = 8
    sizeof (wchar_t) = 4
    success testing <stddef.h>

  • 相关阅读:
    如何用tar和gpg创建压缩加密的档案文件
    如何用Virt-rescue拯救虚拟机?
    如何在Linux中使用命令管理已安装的软件包?
    leetcode TOP100 字母异位词分组
    剑指offer 1-5
    XCTF(MISC) 坚持60s
    XCTF(MISC) give_you_flag
    XCTF MISC 如来十三掌
    XCTF(MISC) 图片隐写
    XCTF csaw2013reversing2
  • 原文地址:https://www.cnblogs.com/Mr-Wenyan/p/7221718.html
Copyright © 2011-2022 走看看