11 1 struct student char name40 int age

  • Slides: 14
Download presentation

11. 1 ตวชกบตวแปรโครงสราง struct student{ char name[40]; int age ; }; struct student s;

11. 1 ตวชกบตวแปรโครงสราง 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

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

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

11. 1 ตวชกบตวแปรโครงสราง ตวอยาง int main(){ struct card { char *face; char *suit; };

11. 1 ตวชกบตวแปรโครงสราง ตวอยาง 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 ); yyyy xxxx return 0; mmmm nnnn } space Ace of spade a. face a. suit Ace of spade nnnn mmmm Ace of spade yyyy xxxx a. Ptr a 5

โปรแกรม 11. 1. โปรแกรมเกบขอมลนศ 10 คน Student[0] name: joy age: 12 Student[1] name: boy

โปรแกรม 11. 1. โปรแกรมเกบขอมลนศ 10 คน 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

โปรแกรม 11. 1. โปรแกรมเกบขอมลนศ 10 คน #include<stdio. h> #include<conio. h> int main() { struct

โปรแกรม 11. 1. โปรแกรมเกบขอมลนศ 10 คน #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

โปรแกรม 11. 1. โปรแกรมเกบขอมลนศ 10 คน for (i=0; i<10; i++) { printf("Student # %dnt.

โปรแกรม 11. 1. โปรแกรมเกบขอมลนศ 10 คน 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

11. 2 ตวชและตวแปรขอความ (Strings) ������������������ #include <stdio. h> char str. A[80] = "A string

11. 2 ตวชและตวแปรขอความ (Strings) ������������������ #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