UrbanPro
true

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;
} SFOO;

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

Submit

Other Lessons for You

What is Safety-Critical Software?
A software whose failure may cause death or injuries to the users called Safety-Critical Software.The software present as part of Aeroplanes, or present in Rail as railway signalling software or Automotive...

Do You Know Size Of Empty Class and Reason?
Size of empty class is always 1 byte. Reason is, in order to differentiate one object to another object, the size of empty class is always 1 byte. See the below c++ code snippet. Here there are three...

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...

Be prepared to get trained--init
Before starting the training,students must be mentally prepared for acceptance of new knowledge. Students must attend training with open minded forgetting the position they are working.This will help...
S

Smartnub Softsolutions

0 0
0

Tress And Its Traversal
Depth First Traversals:(a) Inorder (Left, Root, Right) : 4 2 5 1 3(b) Preorder (Root, Left, Right) : 1 2 4 5 3(c) Postorder (Left, Right, Root) : 4 5 2 3 1 Trees are one of the data structures like...
X

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