Thuta Learning
ရှာဖွေရန်
AdvancedProgrammingbeginner

Structures

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

struct က မတူတဲ့ data type များကို object တစ်ခုလို group ဖွဲ့ပေးတဲ့နည်းလမ်းပါ။ Student တစ်ယောက်မှာ name, age, score ရှိသလို data တွေကို တစ်စုတည်းထားချင်တဲ့အခါ အသုံးဝင်ပါတယ်။

c
#include <stdio.h>

struct Student {
  char name[30];
  int age;
  float score;
};

int main() {
  struct Student s1 = {"Sai", 20, 86.5};

  printf("Name: %s
", s1.name);
  printf("Age: %d
", s1.age);
  printf("Score: %.1f", s1.score);
  return 0;
}

struct Student က custom data structure တစ်ခုဖန်တီးတာပါ။ s1.name, s1.age လို dot operator နဲ့ member value ကိုယူပါတယ်။

You should see
Name: Sai Age: 20 Score: 86.5

Info

Real project မှာ product, user, order, book စတဲ့ related data တွေကို struct နဲ့ model လုပ်နိုင်ပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Struct definition အဆုံးမှာ semicolon ; မေ့တတ်ပါတယ်။ }; နဲ့ပိတ်ရပါမယ်။
Structures | Thuta Learning