10 1 0100 6012 Computer Programming struct student

  • Slides: 16
Download presentation

10. 1 ตวชกบตวแปรโครงสราง 0100 6012 Computer Programming struct student{ char name[40]; int age ;

10. 1 ตวชกบตวแปรโครงสราง 0100 6012 Computer Programming struct student{ char name[40]; int age ; }; struct student s; struct student *s. Ptr; strcpy(s. name, “Somsak”); s. age = 19; s. Ptr = &s; s s. name Somsak xxxx s. age 19 s. Ptr xxxx หากตองการพมพวาชอนกศกษา สามารถทำไดสองวธคอ • printf("%s", s. name); • printf("%s", s. Ptr->name); 3

10. 1 ตวชกบตวแปรโครงสราง struct card { char *face; char *suit; }; struct card a;

10. 1 ตวชกบตวแปรโครงสราง struct card { char *face; char *suit; }; struct card a; struct card *a. Ptr; a. face = “Queen”; a. suit = “heart”; a. Ptr = &a; a 0100 6012 Computer Programming Queen mmmm nnnn heart a. face a. suit mmmm nnnn xxxx a. Ptr xxxx หากตองการพมพวาไพใบนอยในชดไหน สามารถทำไดสองวธคอ • printf("%s", a. suit); • printf("%s", a. Ptr->suit); 4

10. 1 ตวชกบตวแปรโครงสราง ตวอยาง 0100 6012 Computer Programming int main(){ struct card { char

10. 1 ตวชกบตวแปรโครงสราง ตวอยาง 0100 6012 Computer Programming int main(){ struct card { char *face; char *suit; }; // 2, 3, . . , 9, J, Q, K, A // space, heart, diamond, club struct card a; struct card *a. Ptr; a. face = “Ace"; a. suit = “spade"; a. Ptr = &a; printf( "%s%s%sn%s%s%sn", a. face , " of " , a. suit, a. Ptr->face , " of " , a. Ptr->suit, (*a. Ptr). face, " of " , (*a. Ptr). suit ); xxxx return 0; mmmm nnnn } space Ace of spade a. face a. suit Ace of spade nnnn mmmm Ace of spade xxxx a. Ptr a 5

โปรแกรม 10. 1 โปรแกรมเกบขอมลนศ. 10 คน 0100 6012 Computer Programming Student[0] name: joy age:

โปรแกรม 10. 1 โปรแกรมเกบขอมลนศ. 10 คน 0100 6012 Computer Programming Student[0] name: joy age: 12 Student[1] name: boy age: 20 Student[2] name: jo age: 23 Student[3] name: pat age: 21 Student[4] name: ple age: 13 Student[5] name: tom age: 11 Student[6] name: tu age: 25 Student[7] name: tee age: 34 Student[8] name: bat age: 44 Student[9] name: phon age: 33 jo, 23 pat, 21 tu, 25 tee, 34 bat, 44 phon, 33 7

โปรแกรม 10. 1 โปรแกรมเกบขอมลนศ. 10 คน 0100 6012 Computer Programming #include<stdio. h> #include<conio. h>

โปรแกรม 10. 1 โปรแกรมเกบขอมลนศ. 10 คน 0100 6012 Computer Programming #include<stdio. h> #include<conio. h> int main() { struct profile{ char name[20]; int age; } s[10]; int i; struct profile *s. Ptr; s. Ptr = s; 8

โปรแกรม 10. 1 โปรแกรมเกบขอมลนศ. 10 คน 0100 6012 Computer Programming for (i=0; i<10; i++)

โปรแกรม 10. 1 โปรแกรมเกบขอมลนศ. 10 คน 0100 6012 Computer Programming for (i=0; i<10; i++) { printf("Student # %dnt. Name : ", i+1 ); scanf("%s", s. Ptr->name); printf("t. Age: "); scanf("%d", &(s. Ptr->age)); s. Ptr++; } s. Ptr -= 10; for (i=0; i<10; i++) { if ((s. Ptr->age) > 20) printf("n%s, %d", s. Ptr->name, s. Ptr->age); s. Ptr++; } return 0; } 9

10. 2 ตวชและตวแปรขอความ (Strings) 0100 6012 Computer Programming ������������������ #include <stdio. h> char str.

10. 2 ตวชและตวแปรขอความ (Strings) 0100 6012 Computer Programming ������������������ #include <stdio. h> char str. A[80] = "A string to be used for demonstration purposes"; char str. B[80]; int main(void) { char *p. A; char *p. B; puts(str. A); p. A = str. A; puts(p. A); p. B = str. B; putchar('n'); 12

10. 2 ตวชและตวแปรขอความ (Strings) 0100 6012 Computer Programming while (*p. A != '�') {

10. 2 ตวชและตวแปรขอความ (Strings) 0100 6012 Computer Programming while (*p. A != '') { *p. B++ = *p. A++; } *p. B = ''; puts(str. B); return 0; } 13

10. 3 Pointer ซอน Pointer float time = 9. 28; float *pt_time; float **ptt_time;

10. 3 Pointer ซอน Pointer float time = 9. 28; float *pt_time; float **ptt_time; pt_time = &time; time 9. 28 0100 6012 Computer Programming pt_time ptt_time 0100 0250 03 AA ptt_time = &pt_time; float temp 1; temp 1 = *pt_time; temp 1 float temp 2; 9. 28 0510 temp 2 = **ptt_time; temp 2 9. 28 05 C 0 15