Skip to main content

structure

Structure কি?

structure হচ্ছে user-define data type ।

মানে যে প্রোগ্রাম লিখছে তার বানানো data type , কিছু data type তো আগে থেকেই ছিল যেমনঃ int, float, double , char ইত্যাদি । তাহলে আমাদের এটা শিখতে হবে কেন?

কারণ,

আমরা যখন int a; এভাবে ডিক্লেয়ার করছি তখন সেটা মেমোরিতে ব্লক তৈরী করছে এভাবে,

          

   4byte                             

                         a

 

এখন কেউ যদি চাই একটা variable declare করবে , কিন্তু ২টা ব্লক তৈরী হবে

4byte

4byte

                                      

                                   a

মানে variable declare করবে একবার কিন্তু তার মধ্যে থাকবে ২টা

এটা খুব সহজে Array দিয়ে করা যায়

int  a[2];             int            int

4byte

4byte

 

1.Array তে পাশাপাশি মেমোরি ব্লক দখল হয়

2.  আর একই data টাইপের variable থাকে সবগুলা

 

কিন্তু এখন কেউ যদি চাই একবার variable declare করব , কিন্তু একটা integer type আর একটা double type ব্লক তৈরী হবে, তো এইটা কি Array দিয়ে সম্ভব?

                                   int                 double

 4byte

      8byte

                      

 

Array এর খেলা শেষ এইখানে, Array এইরকম কিছু বানাতে পারবে না,

এই কাজটা আমরা structure দিয়ে বানাইতে পারব, এইখানে আমরা আমাদের ইচ্ছা মত data type বানাতে পারি তাই এটাকে ইউজার ডিফাইন ডেটা টাইপ বলে।

 

structure বানাইতে গেলে struct keyword must লাগবে , এটা দিয়ে আমরা বুঝাবো যে আমরা structute বানাতে যাচ্ছি, এর পর নাম দিব

struct Rifat{

 

};

এইযে এই ফরম্যাটে

 

এরপর আমরা এর ভিতরে কিছু ভ্যারিয়েবল declare করব যেগুলা আমাদের প্রয়োজন হবে, যেমনঃ

 

struct Rifat{

        int age;

double cgpa;

};

উপরের ইউজার ডিফাইন ডেটা টাইপ বা structure বানিয়েছি এটার নাম দিয়েছি Rifat, আর এর মধ্যে প্রয়োজনমত কয়েকটা ভ্যারিয়েবল বা ফিল্ড দিয়েছি।

 

structure এর আরেকটা নাম কি বলছি? ইউজার ডিফাইন data type

 

কোনো ভ্যারিয়েবল ডিক্লেয়ার করতে হয় কিভাবে?

আগে data type then variable_name;

                int  a;

একইভাবে আমাদের  বানানো structure,  ঐটাও তো data type

        struct Rifat      r1;

        (data type)     ( variable_name)

 

date type -  struct Rifat

variable -   r1

এখন আমরা যেমনটা চেয়েছিলাম, variable declare করব একটা, but মেমোরি ব্লক তৈরী হবে ২টা , একটা integer type আরেকটা double type

                                age                   cgpa

    4byte

                  8byte

                           

 

                                int                            double type

তো আমরা এই ধরনের প্রয়োজনে structure ইউজ করব

 

structure এর মেম্বার গুলা access করা যায় কিভাবে? structure type এর Variable এর নাম এর সাথে . (dot) দিয়ে

 

#include<stdio.h>

 

struct Rifat{

    int age;

    double cgpa;

};

int main()

{

    struct Rifat r1;    //variable declaration

    printf("Enter age: ");

    scanf("%d",&r1.age);

 

    printf("Enter cgpa: ");

    scanf("%lf",&r1.cgpa);

 

 

    printf("Age: %d\n",r1.age);

    printf("Cgpa: %.2lf\n",r1.cgpa);

}

 

 

আচ্ছা, তাহলে আমাদের data type – ‘struct Rifat’

int a;

int *ptr;   //integer type pointer

 

অন্যান্য ডেটা টাইপের variable ও  হয় , Pointer ও হয়, তাহলে আমাদের বানানো data type এর ও পয়েন্টার হওয়ার কথা না ?

হ্যাঁ পয়েন্টারও হয়

struct Rifat  *ptr; 

*পয়েন্টার যেই টাইপের সেই টাইপের ভ্যারিয়েবলের address রাখতে পারে ।

তাহলে struct Rifat টাইপের পয়েন্টার কার address রাখতে পারবে?

struct Rifat type এর ই

#include<stdio.h>

#include<stdlib.h>

 

struct Rifat{

    int age;

    double cgpa;

};

int main()

{

    struct Rifat *r1,var;    //variable declaration

    //*r1 pointer, var -variable

    //type- struct Rifat

    r1=&var;  //pointer e address rakhlam

 

    r1->age=915;

    r1->cgpa=4.58;

    printf("Age: %d\n",r1->age);

    printf("Cgpa: %.2lf\n",r1->cgpa);

 

    //pointer diye arek vabeo access korte pari

    //but oporer '->' ai rule ta follow korbo amra

 

    (*r1).age=321;

    (*r1).cgpa=3.30;

    printf("Age: %d\n",(*r1).age);

    printf("Cgpa: %.2lf\n",(*r1).cgpa);

 

}


structure এর ভিতরের ভ্যারিয়েবল গুলা pointer দিয়ে  access করতে পয়েন্টারের নাম -> variable

এভাবে করতে হয়।



Comments

Popular posts from this blog

IELTS Spoken Class Adminssion Scenario - 01

.......  Student: Hello, May I come in, sir ? Optional (student): May I sit ? Sir:  Please have a seat. Sir: How may I help you, Sir ? Student: I would like to admit in your spoken course. Sir: Oh sure. Student: How many days are there in a week ? Sir: There are three classes in a week. Student: What time do you offer class ? Sir: We have class at 11am / 4pm / 6pm / 8pm

Php Learning Time

 Differences of explode( ) and implode( ) in php: explode: একটা স্ট্রিংকে কোনো একটা সেপারেটরের বেসিসে অ্যারেতে কনভার্ট করে, যেমন  <?php $text="Hello How are you?"; print_r(explode(" ",$text)); ?> This will give output of  Array (     [0] => Hello     [1] => How     [2] => are     [3] => you? ) Differences of array_splice( ) and array_slice( ) ধরেন আসল অ্যারে হচ্ছে    $arr =[ "Hello" , "this" , "is" , "test" , "text" ];    এখন এটাকে স্লাইসিং করার জন্য আমরা উপরের দুইটা মেথড ই ব্যবহার করতে পারি , কিন্তু array_splice এটা ইউজ করলে অরজিনাল array ও চেঞ্জ হয়ে যাবে, মানে যদি আমরা এভাবে লিখি  $var2 = array_splice ( $arr , 0 , 2 );   তাইলে $var2 এর ভিতরে থাকবে ["Hello" , "this"] ,  আর অরজিনাল array তে বাকি থাকবে ["is", "test", "text"] কিন্তু যদি আমরা ইউজ করি তাহলে অরজিনাল array আগের মতোই থাকবে পাশাপাশি $var2 এর মধ্যে ভ্যালু গুলা এসে পড়বে  Array (     [0] => ...