Array and Record
Array หมายถึงเนื้อที่หน่วยความจำที่เรียงต่อกันใช้เก็บข้อมูลชนิดเดียวกัน เนื้อที่หน่วยความจำนี้มีชื่อเดียวกันแต่แยกตำแหน่งหรือระบุตำแหน่งของข้อมูลแต่ละตัวด้วยการใช้ดรรชนีกำกับ (Index) หรือ Subscript ซึ่งเราสามารถทราบขนาดและมิติ (Dimension) ของ Array เหล่านั้นได้ด้วยการสังเกตที่ดรรชนีกำกับ (Index) หรือ Subscript จะพิจารณาตามประเภทของอะเรย์ในมิติต่างๆ ดังนี้
- อาร์เรย์ 1 มิติ
- อาร์เรย์ 2 มิติ
อาร์เรย์ 1 มิติ คือตัวแปรอาร์เรย์ที่ใช้เก็บข้อมูลเป็นกลุ่ม มีลักษณะตำแหน่งที่ตั้งเป็นลักษณะแถวเดียว(วางตามแนวนอน) หรือคอลัมน์เดียว(วางตามแนวตั้ง)
รูปแบบการประกาศตัวแปรอาร์เรย์มิติเดียว
Data – type array-name [expression]
อาร์เรย์ 2 มิติ คือใช้เก็บข้อมูลเป็นกลุ่ม ที่ข้อมูลนั้นใช้ตัวแปรชื่อเดียวกันแต่ต่างกันที่ตัวห้อย(subscript) ซึ่งจะบอกลำดับความแตกต่างของตัวแปรว่าอยู่ในแถวใด ในคอลัมน์ที่เท่าไร ลำดับใดในแถว
รูปแบบการประกาศตัวแปรอาร์เรย์สองมิติ
Data – type array – name [row size] [column size]
เช่น int a[3][3];
Structure
ตัวอย่างการกำหนดข้อมูลสตรักเจอร์
struct employee
{
char name[20];
int age;
float salary;
}employ ;
----------------------------------------------------------------------------------
#include
struct place
{
char name_of_place[50];
char phone_number[15];
char e_mail[30];
int month;
int year;
};
struct address
{
char city[20];
char country[20];
struct place place1;
} address1;
void input_data()
{
printf("place data\n");
printf("name_of_place=");
scanf("%s",& address1.place1.name_of_place);
printf("phone_number=");
scanf("%s",&address1.place1.phone_number);
printf("e-mail=");
scanf("%s",&address1.place1.e_mail);
printf("day=");
scanf("%d",&address1.place1.day);
printf("month=");
scanf("%d",&address1.place1.month);
printf("year=");
scanf("%d",&address1.place1.year);
printf("city=");
scanf("%s",&address1.city);
printf("country=");
scanf("%s",&address1.country);
}
void show_data()
{
printf("Display Data of place\n");
printf("name_of_place=%s\n",address1.place1.name_of_place);
printf("phone_number= %s\n",address1.place1.phone_number);
printf("e_mail=%s\n",address1.place1.e_mail);
printf("day=%d\n",address1.place1.day);
printf("month=%d\n",address1.place1.month);
printf("year=%d\n",address1.place1.year);
printf("city=%s\n",address1.city);
printf("country=%s\n",address1.country);
}
main()
{
input_data();
show_data();
}