struct CSIM PU C Language q struct mydata

  • Slides: 23
Download presentation
結構(struct) CSIM, PU C Language

結構(struct) CSIM, PU C Language

結構宣告 q 範例 : struct mydata /* 定義結構mydata; declare the structure of mydata*/ {

結構宣告 q 範例 : struct mydata /* 定義結構mydata; declare the structure of mydata*/ { char name[8]; /* 各欄位的內容; the content of mydata */ char id[10]; int math; int eng; }; struct mydata student; CSIM, PU /* 宣告結構mydata型態之變數student */ C Language 4

結 構 宣 告 – the Memory Space q 上頁敘述會在記憶體裡配置空間出來讓 student 結構變數使用,記憶 記憶體 name

結 構 宣 告 – the Memory Space q 上頁敘述會在記憶體裡配置空間出來讓 student 結構變數使用,記憶 記憶體 name id student math eng CSIM, PU C Language 5

Example 1 ---Test Memory Space #include<stdio. h> #include<stdlib. h> int main(void) { struct data

Example 1 ---Test Memory Space #include<stdio. h> #include<stdlib. h> int main(void) { struct data { 一個char佔 1 byte,陣列大小 10,所以佔 10 byte char name[10]; char sex[2]; int math; }; struct data student; printf("sizeof(student)=%dn", sizeof(student)); system("pause"); return 0; } CSIM, PU C Language 6

Example 2 #include<stdio. h> int main(void) { struct mydata /* 定義並宣告結構變數 */ { char

Example 2 #include<stdio. h> int main(void) { struct mydata /* 定義並宣告結構變數 */ { char name[15]; int math; }student; printf(" Student’s name: "); /* 輸入結構變數 */ gets(student. name); printf(" Math score: "); scanf("%d", &student. math); printf("*****Output*****n"); /* 輸出結構變數內容 */ printf("%s’s Math score is %dn", student. name, student. math); system("pause"); return 0; } CSIM, PU C Language 9

初值的設定 q 範例 : struct mydata /* 定義結構mydata */ { char name[15]; /* 各欄位的內容

初值的設定 q 範例 : struct mydata /* 定義結構mydata */ { char name[15]; /* 各欄位的內容 */ char id[10]; int math; int eng; }; struct mydata student={“David Wang”, ” 411003”}; /* 宣告結構mydata型態之變數student, 並設定結構內欄位的初值 */ CSIM, PU C Language 10

Example 3結構變數的初值設定 #include<stdio. h> int main(void) { struct data { char name[10]; int math;

Example 3結構變數的初值設定 #include<stdio. h> int main(void) { struct data { char name[10]; int math; }; struct data student={"Mary Wang", 74}; printf("學生姓名: %sn", student. name); printf("數學成績: %dn", student. math); system("pause"); return 0; } CSIM, PU C Language 11

結構陣列---Example 4 #include<stdio. h> #define MAX 2 int main(void) { int i; struct data

結構陣列---Example 4 #include<stdio. h> #define MAX 2 int main(void) { int i; struct data /*定義結構data */ { char name[10]; int math; }student[MAX]; for(i=0; i<MAX; i++) /*輸入結構陣列student的資料 */ { printf("學生姓名: "); gets(student[i]. name); printf("數學成績: "); scanf("%d", &student[i]. math); fflush(stdin); /*清空緩衝區內的資料 */ } for(i=0; i<MAX; i++) /*輸出結構陣列的內容*/ printf("%s的數學成績=%dn", student[i]. name, student[i]. math); system("pause"); return 0; } CSIM, PU C Language 13

結構指標---Example 5 #include<stdio. h> #include<stdlib. h> int main(void) { struct data { char name[10];

結構指標---Example 5 #include<stdio. h> #include<stdlib. h> int main(void) { struct data { char name[10]; int eng; }student; struct data *ptr; ptr=&student; printf("學生姓名: "); gets(ptr->name); printf("英文成績: "); scanf("%d", &ptr->eng); printf("英文成績=%d", ptr->eng); system("pause"); return 0; } CSIM, PU C Language 17

巢狀結構---Example 6 #include<stdio. h> #include<stdlib. h> int main(void) { struct date /*define date*/ {

巢狀結構---Example 6 #include<stdio. h> #include<stdlib. h> int main(void) { struct date /*define date*/ { int month; int day; }; struct student /*define nested structure---student*/ { char name[10]; int math; struct date birthday; }s 1={“David Li", 80, {2, 10}}; printf("student name: %sn", s 1. name); printf("birthday: %d month, %d dayn", s 1. birthday. month, s 1. birthday. day); printf("math grade: %dn", s 1. math); return 0; } CSIM, PU C Language 19

Example 7 #include <stdio. h>/* 引入檔頭檔 */ #include <stdlib. h> struct PERSON { char

Example 7 #include <stdio. h>/* 引入檔頭檔 */ #include <stdlib. h> struct PERSON { char name[15]; /* 名字 */ int age; /* 年齡 */ struct PERSON * father; /* 父親指標 */ }; int main(void) Tom 35 Mike 12 { int i; 3 2 struct PERSON family[4]={{"Billy", 80, NULL}, /* 結構變數的宣告 */ {"Tom", 35}, /*設定初值*/ {"Mike", 12}} ; /*設定初值*/ family[1]. father = &family[0]; /*設定家人的關係*/ family[2]. father = &family[1]; family[3]. father = &family[2]; for(i=0; i<4; ++i){ printf( "family[%d]=%st, age=%dt ", i, family[i]. name, family[i]. age); if( family[i]. father != NULL ) /* 判斷有無父親 */ printf(", father is %sn", family[i]. father->name ); else printf(", father unknownn"); } system("pause"); return 0; /* <-程式正常結束,傳回 0 */ } CSIM, PU John 1 65 {"John", 65}, Billy 80 null 0 /*設定初值*/ C Language 22