Computer >> Máy Tính >  >> Lập trình >> lập trình C

Viết chương trình C để hiển thị kích thước và độ lệch của các thành phần cấu trúc

Vấn đề

Viết chương trình C để xác định cấu trúc và hiển thị kích thước cũng như hiệu số của các biến thành viên

Cấu trúc - Nó là một tập hợp các biến kiểu dữ liệu khác nhau, được nhóm lại với nhau dưới một tên duy nhất.

Dạng khai báo cấu trúc chung

datatype member1;
struct tagname{
   datatype member2;
   datatype member n;
};

Đây, struct - keyword

tagname - chỉ định tên của cấu trúc

member1, member2 - chỉ định các mục dữ liệu tạo nên cấu trúc.

Ví dụ

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

Các biến cấu trúc

Có ba cách khai báo biến cấu trúc -

Phương pháp 1

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

Phương pháp 2

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

Phương pháp 3

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

Khởi tạo và truy cập cấu trúc

Liên kết giữa một thành viên và một biến cấu trúc được thiết lập bằng cách sử dụng toán tử thành viên (hoặc) toán tử dấu chấm.

Việc khởi tạo có thể được thực hiện theo những cách sau -

Phương pháp 1

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

Phương pháp 2

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

Phương pháp 3 (sử dụng toán tử thành viên)

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

Phương pháp 4 (sử dụng hàm scanf)

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

Khai báo cấu trúc với các thành viên dữ liệu và cố gắng in các giá trị offset của chúng cũng như kích thước của cấu trúc.

Chương trình

#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 'a' is :%d\n",sizeof(t1.a));
   printf("the size 'b' is :%d\n",sizeof(t1.b));
   printf("the size 'c' is :%d\n",sizeof(t1.c));
   printf("the size 'd' is :%d\n",sizeof(t1.d));
   printf("the size 'e' is :%d\n",sizeof(t1.e));
   printf("the offset 'a' is :%d\n",offsetof(struct tutorial,a));
   printf("the offset 'b' is :%d\n",offsetof(struct tutorial,b));
   printf("the offset 'c' is :%d\n",offsetof(struct tutorial,c));
   printf("the offset 'd' is :%d\n",offsetof(struct tutorial,d));
   printf("the offset 'e' is :%d\n\n",offsetof(struct tutorial,e));
   printf("size of the structure tutorial is :%d",sizeof(t1));
   return 0;
}

Đầu ra

the size 'a' is :4
the size 'b' is :4
the size 'c' is :4
the size 'd' is :4
the size 'e' is :8
the offset 'a' is :0
the offset 'b' is :4
the offset 'c' is :8
the offset 'd' is :12
the offset 'e' is :16

size of the structure tutorial is :24