【C++】cstddef中4个定义

本文详细介绍了C++中的几种基本类型,包括size_t、NULL、ptrdiff_t及其使用场景,并解释了offsetof宏的功能和用法。通过示例代码展示了如何使用offsetof宏获取结构体成员的偏移量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

size_t

size_t corresponds to the integral data type returned by the language operator sizeof and is defined in the <cstddef> header file (among others) as an unsigned integral type.
size_t = unsigned int

NULL

This macro expands to a null pointer constant.

A null pointer is generally used to signify that a pointer does not point to any object.
In C++, NULL expands either to 0 or 0L.

在c中null经常被定义为(void)*0,即为空指针,而在C++中null为一整型0。

ptrdiff_t

This is the type returned by the subtraction operation between two pointers. 

两个指针相减之差。

offsetof

offsetof (type,member)

This macro with functional form returns the offset value in bytes of member member in the structure type type.

此函数形式的宏返回member在结构type中的偏移。

#include <stdio.h>   
#include <stddef.h>   
  
struct mystruct 
{  
    char singlechar;  
    char arraymember[10];  
    char anotherchar;  
};  
  
int main ()  
{  
    printf ("offsetof(mystruct,singlechar) is %d\n",offsetof(mystruct,singlechar));  
    printf ("offsetof(mystruct,arraymember) is %d\n",offsetof(mystruct,arraymember));  
    printf ("offsetof(mystruct,anotherchar) is %d\n",offsetof(mystruct,anotherchar));  
    
    return 0;  
} 

转自: http://blog.youkuaiyun.com/pfanaya/article/details/7009041#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值