Home > Backend Development > C++ > Write a C program to display the size and offset of structure members

Write a C program to display the size and offset of structure members

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-08-29 20:09:19
forward
774 people have browsed it

Write a C program to display the size and offset of structure members

Question

Write a C program to define a structure and display the size and offset of member variables

Structure Body - It is a collection of variables of different data types, grouped under one name.

General form of structure declaration

datatype member1;
struct tagname{
   datatype member2;
   datatype member n;
};
Copy after login

Here, struct - keyword

tagname - specifies the name of the structure

member1, member2 - specifies the constituent structure data items.

Example

struct book{
   int pages;
   char author [30];
   float price;
};
Copy after login

Structure variables

There are three ways to declare structure variables-

Method 1

struct book{
   int pages;
   char author[30];
   float price;
}b;
Copy after login

Method 2

struct{
   int pages;
   char author[30];
   float price;
}b;
Copy after login

Method 3

struct book{
   int pages;
   char author[30];
   float price;
};
struct book b;
Copy after login

Initializing and accessing the structure

The link between members and structure variables is established through the member operator (or dot operator).

Can be initialized in the following ways:

Method 1

struct book{
   int pages;
   char author[30];
   float price;
} b = {100, "balu", 325.75};
Copy after login

Method 2

struct book{
   int pages;
   char author[30];
   float price;
};
struct book b = {100, "balu", 325.75};
Copy after login

Method 3 (using member operator)

struct book{
   int pages;
   char author[30];
   float price;
} ;
struct book b;
b. pages = 100;
strcpy (b.author, "balu");
b.price = 325.75;
Copy after login

Method 4 (using scanf function)

struct book{
   int pages;
   char author[30];
   float price;
} ;
struct book b;
   scanf ("%d", &b.pages);
   scanf ("%s", b.author);
   scanf ("%f", &b. price);
Copy after login

Declare the structure using data members and try to print their offset values ​​and the size of the structure.

Program

Real-time demonstration

#include<stdio.h>
#include<stddef.h>
struct tutorial{
   int a;
   int b;
   char c[4];
   float d;
   double e;
};
int main(){
   struct tutorial t1;
   printf("the size &#39;a&#39; is :%d</p><p>",sizeof(t1.a));
   printf("the size &#39;b&#39; is :%d</p><p>",sizeof(t1.b));
   printf("the size &#39;c&#39; is :%d</p><p>",sizeof(t1.c));
   printf("the size &#39;d&#39; is :%d</p><p>",sizeof(t1.d));
   printf("the size &#39;e&#39; is :%d</p><p>",sizeof(t1.e));
   printf("the offset &#39;a&#39; is :%d</p><p>",offsetof(struct tutorial,a));
   printf("the offset &#39;b&#39; is :%d</p><p>",offsetof(struct tutorial,b));
   printf("the offset &#39;c&#39; is :%d</p><p>",offsetof(struct tutorial,c));
   printf("the offset &#39;d&#39; is :%d</p><p>",offsetof(struct tutorial,d));
   printf("the offset &#39;e&#39; is :%d</p><p></p><p>",offsetof(struct tutorial,e));
   printf("size of the structure tutorial is :%d",sizeof(t1));
   return 0;
}
Copy after login

Output

the size &#39;a&#39; is :4
the size &#39;b&#39; is :4
the size &#39;c&#39; is :4
the size &#39;d&#39; is :4
the size &#39;e&#39; is :8
the offset &#39;a&#39; is :0
the offset &#39;b&#39; is :4
the offset &#39;c&#39; is :8
the offset &#39;d&#39; is :12
the offset &#39;e&#39; is :16

size of the structure tutorial is :24
Copy after login

The above is the detailed content of Write a C program to display the size and offset of structure members. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template