কম্পিউটার টিউটোরিয়াল

C প্রোগ্রাম: স্ট্রাকচার মেম্বারের সাইজ ও অফসেট প্রদর্শন


সমস্যা

স্ট্রাকচার ডিফাইন করে মেম্বার ভেরিয়েবলগুলোর সাইজ ও অফসেট প্রদর্শনের C প্রোগ্রাম লিখুন।

স্ট্রাকচার (Structure) — বিভিন্ন ডেটা টাইপের ভেরিয়েবলগুলোর একটি সংগ্রহ যা একক নামের অধীনে গ্রুপ করা হয়।

স্ট্রাকচার ডিক্লেয়ারেশনের সাধারণ ফর্ম

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

এখানে struct — কিওয়ার্ড

tagname — স্ট্রাকচারের নাম নির্দেশ করে

member1, member2 — স্ট্রাকচার বানাতে ডেটা আইটেম নির্দেশ করে।

উদাহরণ

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

স্ট্রাকচার ভেরিয়েবল

স্ট্রাকচার ভেরিয়েবল ডিক্লেয়ার করার তিনটি পদ্ধতি—

পদ্ধতি ১

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

পদ্ধতি ২

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

পদ্ধতি ৩

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

স্ট্রাকচারের ইনিশিয়ালাইজেশন ও অ্যাক্সেস

এক মেম্বার ও স্ট্রাকচার ভেরিয়েবলের লিংক মেম্বার অপারেটর (.) দিয়ে প্রতিষ্ঠিত হয়।

ইনিশিয়ালাইজেশন নিম্নলিখিত পদ্ধতিতে করা যায়—

পদ্ধতি ১

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

পদ্ধতি ২

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

পদ্ধতি ৩ (মেম্বার অপারেটর ব্যবহার করে)

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

পদ্ধতি ৪ (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

  1. সি প্রোগ্রাম দিয়ে দ্বিঘাত সমীকরণের রুট (Roots) বের করা

  2. C#-এ হোস্টনেম এবং IP অ্যাড্রেস প্রদর্শন

  3. জাভা প্রোগ্রামের স্ট্রাকচার এবং মেম্বাররা

  4. প্রদত্ত DataFrame-এ ইন্ডেক্স এবং কলাম ট্রান্সপোজ (transpose) করার পাইথন প্রোগ্রাম লিখুন