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

Advantages of C++ Language
Advantages of C++ - C++ is a profoundly convenient dialect and is frequently the dialect of decision for multi-gadget, multi-stage application advancement. - C++ is a protest situated programming dialect...

Predict the Output of C Questions based on Recursion and Macro
#include<stdio.h>void k(unsigned int i,unsigned int j){ static char x="0123456789ABCDEF"; if(i>=j) k(i/j,j); putchar(x);}void main(){ k(20,12);} ___________________________________________ ...

Compiling a C program on Linux machine GCC
Note: if GCC is not available in your Ubuntu Linux machine install by using below command $sudo apt-get install gcc-4.9 Here 4.9 is GCC version Similarly for installing vim $ sudo apt-get install vim ...
N

Narasimha Reddy

0 0
0

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

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