সমস্যা
গঠন সংজ্ঞায়িত করার জন্য একটি সি প্রোগ্রাম লিখুন এবং সদস্য ভেরিয়েবলের আকার এবং অফসেটগুলি প্রদর্শন করুন
গঠন − এটি বিভিন্ন ডেটাটাইপ ভেরিয়েবলের একটি সংগ্রহ, যা একটি একক নামের অধীনে একত্রিত হয়৷
৷গঠন ঘোষণার সাধারণ রূপ
datatype member1;
struct tagname{
datatype member2;
datatype member n;
}; এখানে, struct - কীওয়ার্ড
ট্যাগনেম - কাঠামোর নাম নির্দিষ্ট করে
member1, member2 - ডাটা আইটেম নির্দিষ্ট করে যা গঠন করে।
উদাহরণ
struct book{
int pages;
char author [30];
float price;
}; স্ট্রাকচার ভেরিয়েবল
গঠন ভেরিয়েবল ঘোষণা করার তিনটি উপায় আছে −
পদ্ধতি 1
struct book{
int pages;
char author[30];
float price;
}b; পদ্ধতি 2
struct{
int pages;
char author[30];
float price;
}b; পদ্ধতি 3
struct book{
int pages;
char author[30];
float price;
};
struct book b; প্রবর্তন এবং কাঠামো অ্যাক্সেস করা
সদস্য অপারেটর (বা) ডট অপারেটর ব্যবহার করে সদস্য এবং একটি কাঠামো পরিবর্তনশীলের মধ্যে সংযোগ স্থাপন করা হয়।
সূচনা নিম্নলিখিত উপায়ে করা যেতে পারে -
পদ্ধতি 1
struct book{
int pages;
char author[30];
float price;
} b = {100, "balu", 325.75}; পদ্ধতি 2
struct book{
int pages;
char author[30];
float price;
};
struct book b = {100, "balu", 325.75}; পদ্ধতি 3 (সদস্য অপারেটর ব্যবহার করে)
struct book{
int pages;
char author[30];
float price;
} ;
struct book b;
b. pages = 100;
strcpy (b.author, "balu");
b.price = 325.75; পদ্ধতি 4 (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); ডেটা সদস্যদের সাথে কাঠামো ঘোষণা করুন এবং তাদের অফসেট মানগুলির পাশাপাশি কাঠামোর আকার প্রিন্ট করার চেষ্টা করুন৷
প্রোগ্রাম
#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;
} আউটপুট
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