Home  >  Article  >  Backend Development  >  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

WBOY
WBOYforward
2023-08-29 20:09:19730browse

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

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

Structure variables

There are three ways to declare structure variables-

Method 1

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

Method 2

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

Method 3

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

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

Method 2

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

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;

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);

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

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

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete