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

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

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

Importance Of Function Prototype In C
Function prototype tells compiler about number of parameters function takes, data-types of parameters and return type of function. By using this information, compiler cross checks function parameters and...

C language
#include #include void main() { clrscr(); printf("\n Urban Pro "); getch(); } Answer the output in c programming: Output: UrbanPro

C Programming Introduction
Beginners: C Programming is the base of all programming languages we learn. This Page will help you to understand all difficulties you may face in writing a simple program. What to remember while...
N

Namrata Y.

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