通用定义:stddef.h

作者:追风剑情 发布于:2020-6-29 9:37 分类:C

该头文件定义了一些类型和宏。

stddef.h类型
类型 描述
ptrdiff_t 有符号整数类型,表示两个指针之差
size_t 有符号整数类型,表示sizeof运算符的结果
wchar_t 整数类型,表示支持的本地化所指定的最大扩展字符集
NULL 实现定义的常量,表示空指针
offsetof(type, member-designator) 展开为size_t类型的值,表示type类型结构的指定成员在该结构中的偏移量,以字节为单位。如果成员是一个位字段,该宏的行为是未定义的

示例:offsetof()

//Microsoft Visual Studio
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stddef.h>

struct car
{
	char brand[30];
	char model[30];
	double hp;
	double price;
};

int main(int argc, char* argv[])
{
	//hp成员的偏移量
	size_t brand_offset = offsetof(struct car, brand);
	size_t model_offset = offsetof(struct car, model);
	size_t hp_offset = offsetof(struct car, hp);
	size_t price_offset = offsetof(struct car, price);
	printf("brand_offset=%d, model_offset=%d, hp_offset=%d, price_offset=%d\n",
		brand_offset, model_offset, hp_offset, price_offset);

	system("pause");
	return 0;
}

运行测试
111.png

标签: C语言

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号