งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

ข้อมูลชนิดโครงสร้าง (Structure Data)

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "ข้อมูลชนิดโครงสร้าง (Structure Data)"— ใบสำเนางานนำเสนอ:

1 ข้อมูลชนิดโครงสร้าง (Structure Data)
Chapter 8 ข้อมูลชนิดโครงสร้าง (Structure Data) Introduction to Programming Department of Computer Business

2 ข้อมูลชนิดโครงสร้าง (Structure Data)
เป็นการรวมกลุ่มของตัวแปรชนิดต่าง ๆ ที่มีความสัมพันธ์หรือเกี่ยวข้องกันรวมเข้าด้วยกัน โดยมีชื่อตัวแปรใหม่เพียงชื่อเดียว เพื่อความสะดวกใน การใช้งานกับข้อมูลที่มีความซับซ้อน และ มีขนาดใหญ่ เช่น ต้องการเก็บประวัติของนักเรียน เป็นต้น Introduction to Programming Department of Computer Business

3 ข้อมูลชนิดโครงสร้าง (Structure Data)
รูปแบบ struct structure_name { type variablename1; type variablename2; ... type variablenameN; } structure_variable; เมื่อ structure_name คือ ชื่อของข้อมูลชนิดโครงสร้าง type คือ ชนิดของตัวแปรที่เป็นสมาชิก variablename คือ ชื่อตัวแปรที่เป็นสมาชิก structure_variable คือ ชื่อตัวแปรชนิดโครงสร้าง Introduction to Programming Department of Computer Business

4 ข้อมูลชนิดโครงสร้าง (Structure Data)
Example struct student_history{ int id; char name[20]; int age; char *address; char *faculty; }student; Introduction to Programming Department of Computer Business

5 ข้อมูลชนิดโครงสร้าง (Structure Data)
การเรียกใช้ การอ้างถึง และการกำหนดค่าข้อมูล การอ้างถึงตัวแปรที่เป็นสมาชิกในตัวแปรโครงสร้างนั้น สามารถทำได้โดยการระบุชื่อตัวแปรโครงสร้าง ตามด้วยจุด (.) และตามด้วยชื่อตัวแปรสมาชิกที่ต้องการอ้างถึง structure_variable.variablename เช่น student.name = “sila chunwijitra”; Introduction to Programming Department of Computer Business

6 ข้อมูลชนิดโครงสร้าง (Structure Data)
การกำหนดค่าเริ่มต้น สามารถทำได้เช่นเดียวกับตัวแปรทั่ว ไป เพียงแต่การอ้างถึงแตกต่างกันเท่านั้น เช่น scanf(“%d”,student.id); gets(student.address); student.faculty = “computer business”; Introduction to Programming Department of Computer Business

7 ข้อมูลชนิดโครงสร้าง (Structure Data)
#include <stdio.h> void main() { struct history{ int id; char name[20]; char *faculty; }student; printf(“Enter id,name,faculty : “); scanf(“%d”,&student.id); scanf(“%s”,student.name); scanf(“%s”,student.faculty); printf(“\nstudent detail”); printf(”\n\t ID : %d \n\t Name : %s \n\t Faculty : %s”,student.id, student.name,student.faculty); } Introduction to Programming Department of Computer Business

8 ข้อมูลชนิดโครงสร้าง (Structure Data)
อาเรย์ของตัวแปรโครงสร้าง (Array of Structure) การกำหนดตัวแปรโครงสร้างให้เป็นอาเรย์นั้น สามารถทำได้เหมือนกับตัว แปรทั่วไป รูปแบบ struct structurename structurevariable[size]; เช่น struct history student[100]; Introduction to Programming Department of Computer Business

9 ข้อมูลชนิดโครงสร้าง (Structure Data)
การอ้างถึงข้อมูลของอาเรย์ของตัวแปรโครงสร้างเหมือนกับการอ้างถึงข้อมูลในตัวแปรโครงสร้างทั่วไป เพียงแต่ต้องระบุตำแหน่งของข้อมูลในตัวแปรโครงสร้างที่ต้องการเท่านั้น เช่น student[0].id = 100; printf(“student name : %s“,student[10].name); Introduction to Programming Department of Computer Business

10 ข้อมูลชนิดโครงสร้าง (Structure Data)
การส่งตัวแปรโครงสร้างให้กับฟังก์ชัน สามารถส่งค่าให้กับฟังก์ชันได้ 3 แบบ คือ 1. ส่งตัวแปรสมาชิกที่อยู่ในตัวแปรโครงสร้างไปยังฟังก์ชัน 2. ส่งพอยน์เตอร์ของตัวแปรโครงสร้างไปยังฟังก์ชัน 3. ส่งตัวแปรโครงสร้างทั้งหมดไปยังฟังก์ชัน Introduction to Programming Department of Computer Business

11 ข้อมูลชนิดโครงสร้าง (Structure Data)
เป็นการส่งค่าของตัวแปรสมาชิกไปยังฟังก์ชัน เหมือนกับ การส่งค่าจากตัวแปรทั่วไป argument ของฟังก์ชันที่ใช้รับค่าก็เป็น ตัวแปรทั่วไป หรือเป็นการส่งแบบ Call by value เช่น function1(student.id); function2(student.name,student.faculty); Introduction to Programming Department of Computer Business

12 ข้อมูลชนิดโครงสร้าง (Structure Data)
Example #include <stdio.h> void display(int id,int age); void main() { struct history{ int id; char name[20]; int age; }student; printf(“Enter id,name,age : “); scanf(“%d”,&student.id); scanf(“%s”,student.name); scanf(“%d”,&student.age); display(student.id,student.age); } Introduction to Programming Department of Computer Business

13 ข้อมูลชนิดโครงสร้าง (Structure Data)
void display(int id,int age) { printf(“\nStudent ID = %d”,id); printf(“\nAge = %d”,age); } Introduction to Programming Department of Computer Business

14 ข้อมูลชนิดโครงสร้าง (Structure Data)
ส่งพอยน์เตอร์ของตัวแปรโครงสร้างไปยังฟังก์ชัน เป็นการส่งที่อยู่ในหน่วยความจำของตัวแปรสมาชิกไปยังฟังก์ชัน ฉะนั้น argument ของฟังก์ชันที่ใช้รับค่าจะต้องเป็นตัวแปรพอยน์เตอร์ หรือเป็นการส่งแบบ Call by reference เช่น function1(&student.id); function2(&student.name,&student.faculty); Introduction to Programming Department of Computer Business

15 ข้อมูลชนิดโครงสร้าง (Structure Data)
Example #include <stdio.h> void display(int *id,int *age); void main() { struct history{ int id; char name[20]; int age; }stdudent; printf(“Enter id,name,faculty : “); scanf(“%d”,&student.id); gets(student.name); scanf(“%d”,&student.age); display(&student.id,&student.age); } Introduction to Programming Department of Computer Business

16 ข้อมูลชนิดโครงสร้าง (Structure Data)
void display(int *id,int *age) { printf(“\nStudent ID = %d”,*id); printf(“\nAge = %d”,*age); } Introduction to Programming Department of Computer Business

17 ข้อมูลชนิดโครงสร้าง (Structure Data)
ส่งตัวแปรโครงสร้างทั้งหมดไปยังฟังก์ชัน เป็นการส่งข้อมูลของสมาชิกทั้งหมดในตัวแปรโครงสร้างไปยังฟังก์ชัน ในครั้งเดียวกัน ฉะนั้น argument ของฟังก์ชันที่ใช้รับค่าจะต้องเป็นตัวแปรโครงสร้างเช่นเดียวกัน และมีขนาดและโครงสร้างที่เหมือนกันกับตัวแปรโครงสร้างทางด้านส่ง เช่น function1(student); Introduction to Programming Department of Computer Business

18 ข้อมูลชนิดโครงสร้าง (Structure Data)
Example #include <stdio.h> struct history{ int id; char name[20]; int age; }student; void display(struct history st); void main() { printf(“Enter id,name,age : “); scanf(“%d”,&student.id); scanf(“%s”,student.name); scanf(“%d”,&student.age); display(student); } Introduction to Programming Department of Computer Business

19 ข้อมูลชนิดโครงสร้าง (Structure Data)
void display(struct history st) { printf(“\nStudent ID = %d”,st.id); printf(“\nName = %s”,st.name); printf(“\nAge = %d”,st.age); } Introduction to Programming Department of Computer Business


ดาวน์โหลด ppt ข้อมูลชนิดโครงสร้าง (Structure Data)

งานนำเสนอที่คล้ายกัน


Ads by Google