Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structures Structures and Pointers Structures and Functions.

Similar presentations


Presentation on theme: "Structures Structures and Pointers Structures and Functions."— Presentation transcript:

1 Structures Structures and Pointers Structures and Functions

2 Structure Definition Define a structure book struct book { char title[25]; char author[2][25]; int pages; double price; }; Define a variable Fiction and intialize struct book Fiction = {“A Time to Kill”, {“Arthur Smith”, “John Clark”}, 556, 19.95}; Dr. Sadık EşmelioğluCENG 1142

3 &Fiction.price 70000A70025A 70050J 70075 556 70001 70026 r70051 o70076 70002T70027 t70052 h70077 70003i 70028 h70053 n70078 70004m 70029 u70054 70079 19.95 70005 e70030 r70055 C70080 70006 70031 70056 l70081 70007 t70032 S70057 a70082 70008 o70033 m70058 r70083 70009 70034 I70059 k70084 70010 K70035 t70060 \070085 70011 I70036 h70061 70086 70012 l70037 \070062 70087 70013 l70038 70063 70088 70014 \070039 70064 70089 70015 70040 70065 70090 70016 70041 70066 70091 70017 70042 70067 70092 70018 70043 70068 70093 70019 70044 70069 70094 70020 70045 70070 70095 70021 70046 70071 70096 70022 70047 70072 70097 70023 70048 70073 70098 70024 70049 70074 70099 Memory Allocation for Fiction &Fiction Fiction.title Fiction.author[0] Fiction.author[1] &Fiction.pages

4 Accessing Elements using Pointers Dr. Sadık EşmelioğluCENG 1144 ElementTypeValueAddressPhys Address Fiction.titlestringA time to killFiction.title70000 Fiction.author[0]stringArthur SmithFiction.author[0]70025 Fiction.author[1]stringJohn ClarkFiction.author[1]70050 Fiction.pagesint556&Fiction.pages70075 Fiction.pricedouble19.95&Fiction.price70079 whole structurebook&Fiction70000 struct book Fiction; struct book *ptr; ptr = &Fiction; ptrbook70000&ptr80000 Fiction.title  (*ptr).title  ptr->title Fiction.author[0]  (*ptr).author[0]  ptr->author[0] Fiction.price  (*ptr).price  ptr->price

5 Example Dr. Sadık EşmelioğluCENG 1145 #include struct book { char Title[25]; char Auth[2][20]; int Pgs; double Prc; }; typedef struct book Book_t; int main(void) { struct book bk1 = { "Calculus for Sophmores", {"John Thompson", "Carrie Thompson"}, 787, 89.95 }; #include struct book { char Title[25]; char Auth[2][20]; int Pgs; double Prc; }; typedef struct book Book_t; int main(void) { struct book bk1 = { "Calculus for Sophmores", {"John Thompson", "Carrie Thompson"}, 787, 89.95 };

6 Example Dr. Sadık EşmelioğluCENG 1146 struct book *bkptr; bkptr = &bk1; printf("Size of Title: %d\n", sizeof(bk1.Title)); printf("Size of Auth 0: %d\n", sizeof(bk1.Auth[0])); printf("Size of Auth 0: %d\n", sizeof(bk1.Auth[1])); printf("Size of int: %d\n", sizeof(int)); printf("Size of double: %d\n", sizeof(double)); printf("Size of structure book is: %d\n", sizeof(Book_t)); puts(""); printf("Address of bk1 : %d\n", &bk1); printf("Address of bkptr : %d\n", &bkptr); printf("Value of bkptr : %d\n", bkptr); printf("Value pointed by bk1 : %s\n", &bk1); printf("Value pointed by bkptr: %s\n", bkptr); printf("Value pointed by bkptr: %c\n", *bkptr); puts(""); struct book *bkptr; bkptr = &bk1; printf("Size of Title: %d\n", sizeof(bk1.Title)); printf("Size of Auth 0: %d\n", sizeof(bk1.Auth[0])); printf("Size of Auth 0: %d\n", sizeof(bk1.Auth[1])); printf("Size of int: %d\n", sizeof(int)); printf("Size of double: %d\n", sizeof(double)); printf("Size of structure book is: %d\n", sizeof(Book_t)); puts(""); printf("Address of bk1 : %d\n", &bk1); printf("Address of bkptr : %d\n", &bkptr); printf("Value of bkptr : %d\n", bkptr); printf("Value pointed by bk1 : %s\n", &bk1); printf("Value pointed by bkptr: %s\n", bkptr); printf("Value pointed by bkptr: %c\n", *bkptr); puts("");

7 Example Dr. Sadık EşmelioğluCENG 1147 printf("%s\n", bk1.Title); printf("%s\n", (*bkptr).Title); printf("%s\n", bkptr->Title); printf("%.2f\n", bk1.Prc); printf("%.2f\n", (*bkptr).Prc); printf("%.2f\n", bkptr->Prc); puts(""); printf("Please enter a new Title: "); gets(bkptr->Title); printf("%s\n", bkptr->Title); printf("Please enter a new Price: "); scanf("%lf", &bkptr->Prc); printf("%.2f\n", bkptr->Prc); return(0); } printf("%s\n", bk1.Title); printf("%s\n", (*bkptr).Title); printf("%s\n", bkptr->Title); printf("%.2f\n", bk1.Prc); printf("%.2f\n", (*bkptr).Prc); printf("%.2f\n", bkptr->Prc); puts(""); printf("Please enter a new Title: "); gets(bkptr->Title); printf("%s\n", bkptr->Title); printf("Please enter a new Price: "); scanf("%lf", &bkptr->Prc); printf("%.2f\n", bkptr->Prc); return(0); }

8 Example 2 #include struct book { char title[25]; char author[2][25]; int pages; double price; }; int main(void) { struct book Fiction = { "A Time to Joy", {"Arthur Smith", "John Clark"}, 556, 19.95 }; struct book *bkp; printf("%s\n", bkp->title); printf("%s & %s\n", bkp->author[0], bkp->author[1]); printf("%d\n", bkp->pages); printf("%.2f\n", bkp->price); return(0); }

9 Example 3 #include struct student { intNo; charFname[15], Lname[15]; intQgrds[12]; intQGrd; }; int main(void) { struct student std; struct student *p; int i, j; int temp; printf("Enter std no: "); scanf("%d", &p->No); fflush(stdin); printf("Enter std first name: "); gets(p->Fname); fflush(stdin); printf("Enter std last name: "); gets(p->Lname); for(i=0; i<12; i++) { printf("Enter quiz grade %2d: ", i+1); scanf("%d", &p->Qgrds[i]); }

10 Example 3 for(i=0; i<11; i++) { for(j=0; j<11-i; j++) { if(p->Qgrds[j] Qgrds[j+1]) { temp = p->Qgrds[j]; p->Qgrds[j] = p->Qgrds[j+1]; p->Qgrds[j+1] = temp; } p->QGrd = 0; for(i=0; i<10; i++) p->QGrd += p->Qgrds[i]; printf("%-12d", p->No); printf("%-15s", p->Fname); printf("%-15s", p->Lname); printf("%3d\n", p->QGrd); return(0); }

11 Structure Inp/Outp in Functions Assume the following structure struct student { charfname[15]; charlname[15]; intno; doubleGPA; } ; Dr. Sadık EşmelioğluCENG 11411 a) Structure as input b) Structure as output c) Structure as input and output d) Structure as output (return) e) Structure element as input f) Structure element as output g) Structure element as output (return)

12 Structure as input Dr. Sadık EşmelioğluCENG 11412 Function call void PrtStd(struct student std) { printf(“%s ”, std.fname); printf(“%s “, std.lname); printf(“\t%d“, std.stdno); printf(“\t%.2f\n”, std.GPA); } void PrtStd(struct student std) { printf(“%s ”, std.fname); printf(“%s “, std.lname); printf(“\t%d“, std.stdno); printf(“\t%.2f\n”, std.GPA); } int main(void) { struct student s = { "Jane", /* fname */ "Doe", /* lname */ 201211333,/* no */ 3.7/* GPA */ }; PrtStd(s); return( 0 ); } int main(void) { struct student s = { "Jane", /* fname */ "Doe", /* lname */ 201211333,/* no */ 3.7/* GPA */ }; PrtStd(s); return( 0 ); } Function Definition

13 Structure as output Dr. Sadık EşmelioğluCENG 11413 Function call void FillStd(struct student *std) { strcpy(std->fname, "John"); strcpy(std->lname, "Smith"); std->no = 201011444; std->GPA = 3.5; } void FillStd(struct student *std) { strcpy(std->fname, "John"); strcpy(std->lname, "Smith"); std->no = 201011444; std->GPA = 3.5; } int main(void) { struct student s; FillStd(&s); return( 0 ); } int main(void) { struct student s; FillStd(&s); return( 0 ); } Function Definition

14 Structure as input and output Dr. Sadık EşmelioğluCENG 11414 Function call void ChngGrd(struct student *std) { std->GPA *= (5.0/4.0); } void ChngGrd(struct student *std) { std->GPA *= (5.0/4.0); } int main(void) { struct student s; ChngGrd(&s); return( 0 ); } int main(void) { struct student s; ChngGrd(&s); return( 0 ); } Function Definition

15 Structure as output (return) Dr. Sadık EşmelioğluCENG 11415 Function call struct student FillStd2(void) { struct student std; strcpy(std.fname, "Adam"); strcpy(std.lname, "Johnston"); std.no = 201111222; std.GPA = 2.5; return(std); } struct student FillStd2(void) { struct student std; strcpy(std.fname, "Adam"); strcpy(std.lname, "Johnston"); std.no = 201111222; std.GPA = 2.5; return(std); } int main(void) { struct student s; s= FillStd2(); return( 0 ); } int main(void) { struct student s; s= FillStd2(); return( 0 ); } Function Definition

16 Structure Element as input Dr. Sadık EşmelioğluCENG 11416 Function call void PrtGPA(double Grd) { printf(“The GPA is: %d\n”, Grd); } void PrtGPA(double Grd) { printf(“The GPA is: %d\n”, Grd); } int main(void) { struct student s = { "Jane", /* fname */ "Doe", /* lname */ 201211333,/* no */ 3.7/* GPA */ }; PrtGPA(s.GPA); return( 0 ); } int main(void) { struct student s = { "Jane", /* fname */ "Doe", /* lname */ 201211333,/* no */ 3.7/* GPA */ }; PrtGPA(s.GPA); return( 0 ); } Function Definition

17 Structure Element as output Dr. Sadık EşmelioğluCENG 11417 Function call void ChngGPA(double *Grd) { *Grd = 4.0; } void ChngGPA(double *Grd) { *Grd = 4.0; } int main(void) { struct student s = { "Jane", /* fname */ "Doe", /* lname */ 201211333,/* no */ 3.7/* GPA */ }; PrtGPA(&s.GPA); return( 0 ); } int main(void) { struct student s = { "Jane", /* fname */ "Doe", /* lname */ 201211333,/* no */ 3.7/* GPA */ }; PrtGPA(&s.GPA); return( 0 ); } Function Definition

18 Structure Element as output w/Return Dr. Sadık EşmelioğluCENG 11418 Function call double SetGPA(void) { return(2.89); } double SetGPA(void) { return(2.89); } int main(void) { struct student s = { "Jane", /* fname */ "Doe", /* lname */ 201211333,/* no */ 3.7/* GPA */ }; s.GPA = SetGPA(); return( 0 ); } int main(void) { struct student s = { "Jane", /* fname */ "Doe", /* lname */ 201211333,/* no */ 3.7/* GPA */ }; s.GPA = SetGPA(); return( 0 ); } Function Definition


Download ppt "Structures Structures and Pointers Structures and Functions."

Similar presentations


Ads by Google