OFFSet_offtheroad

OFFSet_offtheroad#defineoffsetof(s,m)(size_t)&reinterpret_cast<constvolatilechar&>((((s*)0)->m))该宏用于求结构体中一个成员在该结构体中的偏移量。第一个参数是结构体的名字,第二个参数是结构体成员的名字。该宏返回结构体structNames中成员memberName(m)的偏移量。偏移量…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

#define offsetof(s,m) (size_t)&reinterpret_cast<const volatile char&>((((s *)0)->m))

该宏用于求结构体中一个成员在该结构体中的偏移量。第一个参数是结构体的名字,第二个参数是结构体成员的名字。该宏返回结构体structName s中成员memberName(m)的偏移量。偏移量是size_t类型的。

offsetof returns the offset in bytes of the specified member from the beginning of its parent data structure. It is undefined for bit fields.

示例

#include <stdio.h>
#include <stddef.h>
typedef struct
{
int iVal;
int iVal2;
}Test;
typedef struct
{
char ch;
int iNum;
}Test2;
int main(void)
{
Test t = {1, 2};
Test2 t2 = {'t', 100};
printf("\naddress of t : %p\naddress of t.iVal : %p\naddress of t.iVal2: %p\n\n", &t, &(t.iVal), &(t.iVal2));
printf("offset of iVal in t: %p\n", offsetof(Test, iVal));
printf("offset of iVal2 in t: %p\n", offsetof(Test, iVal2));
printf("\naddress of t2 : %p\naddress of t2.ch : %p\naddress of t2.iNum: %p\n\n", &t, &(t2.ch), &(t2.iNum));
printf("offset of ch in t2: %p\n", offsetof(Test2, ch));
printf("offset of iNum in t2: %p\n", offsetof(Test2, iNum));
return 0;
}

 

  wps_clip_image-31628

 

  注意内存对齐。

原文

http://www.cppblog.com/lovedday/archive/2007/09/24/32801.html

http://baike.baidu.com/view/5513779.htm

转载于:https://www.cnblogs.com/mydomain/archive/2013/06/02/3114010.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/172089.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Android Studio入门到精通[通俗易懂]

    Android Studio入门到精通[通俗易懂]PS一句:最终还是选择CSDN来整理发表这几年的知识点,该文章平行迁移到CSDN。因为CSDN也支持MarkDown语法了,牛逼啊!目标:AndroidStudio新手–>下载安装配置–>零基础入门–>基本使用–>调试技能–>构建项目基础–>使用AS应对常规应用开发AS简介经过2年时间的研发,Google终于正式发布了面向Android开发者的集成开发环境AndroidStudio1.2(稳定

    2022年6月5日
    36
  • ubuntu安装Microsoft Edge并设置为中文

    ubuntu安装Microsoft Edge并设置为中文1.Linux版本的Edge两种安装方式:下载.deb安装官方下载网址MicrosoftEdgeInsiderChannelssudodpkg-imicrosoft-edge-dev_91.0.864.1-1_amd64.deb命令行安装##Setupcurlhttps://packages.microsoft.com/keys/microsoft.asc|gpg–dearmor>microsoft.gpgsudoinstall-oroo

    2022年7月21日
    19
  • Spring面试题(2020最新版)「建议收藏」

    Spring面试题(2020最新版)「建议收藏」文章目录Spring概述(10)什么是spring?Spring框架的设计目标,设计理念,和核心是什么Spring的优缺点是什么?Spring有哪些应用场景Spring由哪些模块组成?Spring框架中都用到了哪些设计模式?详细讲解一下核心容器(springcontext应用上下文)模块Spring框架中有哪些不同类型的事件Spring应用程序有哪些不同组件?使用Spring有哪些方式…

    2022年5月7日
    34
  • springboot项目搭建流程_spring boot 项目

    springboot项目搭建流程_spring boot 项目文章目录1.问题描述2.解决方案2.1新建Springboot项目(1)file->new->project(2)点击next(第一个)(3)点击next(第二个)(4)点击next(第三个)2.2springboot默认生成三个文件2.2.1.pom.xml2.2.2application.properties2.2.3启动类文件(SptestApplication.j…

    2022年10月13日
    1
  • PHP条件语句if的使用

    PHP条件语句if的使用

    2021年10月9日
    44
  • 各国手机号码正则

    各国手机号码正则’ar-DZ’:/^(\+?213|0)(5|6|7)\d{8}$/,’ar-SY’:/^(!?(\+?963)|0)?9\d{8}$/,’ar-SA’:/^(!?(\+?966)|0)?5\d{8}$/,’en-US’:/^(\+?1)?[2-9]\d{2}[2-9](?!11)\d{6}$/,’cs-CZ’:/^(\+?420)??[1-9][0-9]{2}?[0-9]{3}?[0-9]{3}$/,’de-DE’:/^(\+?4…

    2022年5月29日
    41

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号