Learn C++ Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Magic in C=> OFFSET OF MACRO in C [offsetof(TYPE, MEMBER)]

Jyotishkar D.
20/05/2019 0 0

Here I am going to discuss an exciting macro in C. It will give you offset of a member within a structure.

NULL or 0 address is typecasted to TYPE first. It won't result in segmentation fault as we are not going to access the value at the NULL address. By typecasting NULL to TYPE, you can imagine that an object of type TYPE is residing at 0 address location. Now, after that, if you access the member's address, it will give you the offset. Since we are typecasting 0 address location to TYPE, we are forcing the compiler to think that an object of type TYPE exists there, then afterwards we are reading the address of that member. It will give you the offset because the member's address is calculated relative to 0. Since we are not accessing memory at that location and merely fetching the address, it won't result in segmentation fault due to invalid memory access.

typedef struct
int i;
float f;
char c;

void main(void)
printf("Offset of 'f' is %u", offsetof(SFOO, f));

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

0 Dislike
Follow 3

Please Enter a comment


Other Lessons for You

Super-Market Billing Console Application
#include using namespace std; int main() { int n=0; float T=0; cout<<"Welcome to Yashtosh Mart !\n"<<endl; cout<<"Type total number of items bought"<<endl; cin>>n;...

Definite book guides for C++
This post attempts to collect the few pearls among the dozens of bad C++ books. Unlike many other programming languages, which are often picked up on the go from tutorials found on the internet, only...

C++ Advanced Course Study With C++11 Features
OOPS(Object Oriented Programming Systems) In C++. Types of Languages 2GL(Basic,Instruction programming to microprocessors/MC), 3GL(COBOL(= EQUALS TO),C++,C,PASCAL,Java,C#) 4GL(HTML,CSS,JSON) Style...

Basics Of Embedded System
TOPICS TO BE DISCUSSED System Embedded System Components Classifications Processors Other Hardware Software INTRODUCTION: What is a system?, A system is a way of working, organizing or...

Xplora Technologies

0 0

C Programming basics
C PROGRAMMING BASICS 10 Hours Introduction to ‘ C’ programming –fundamentals – structure of a ‘C’...

Om K.

0 0

Looking for C++ Language Classes?

The best tutors for C++ Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C++ Language with the Best Tutors

The best Tutors for C++ Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more