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 r70051 o T70027 t70052 h i h70053 n m u e70030 r70055 C l t70032 S70057 a o70033 m70058 r I70059 k K70035 t70060 \ I70036 h l70037 \ l \ 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, }; #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, };

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, }; 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 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 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 */ ,/* no */ 3.7/* GPA */ }; PrtStd(s); return( 0 ); } int main(void) { struct student s = { "Jane", /* fname */ "Doe", /* lname */ ,/* no */ 3.7/* GPA */ }; PrtStd(s); return( 0 ); } Function Definition

13 Structure as output Dr. Sadık EşmelioğluCENG Function call void FillStd(struct student *std) { strcpy(std->fname, "John"); strcpy(std->lname, "Smith"); std->no = ; std->GPA = 3.5; } void FillStd(struct student *std) { strcpy(std->fname, "John"); strcpy(std->lname, "Smith"); std->no = ; 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 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 Function call struct student FillStd2(void) { struct student std; strcpy(std.fname, "Adam"); strcpy(std.lname, "Johnston"); std.no = ; std.GPA = 2.5; return(std); } struct student FillStd2(void) { struct student std; strcpy(std.fname, "Adam"); strcpy(std.lname, "Johnston"); std.no = ; 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 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 */ ,/* no */ 3.7/* GPA */ }; PrtGPA(s.GPA); return( 0 ); } int main(void) { struct student s = { "Jane", /* fname */ "Doe", /* lname */ ,/* no */ 3.7/* GPA */ }; PrtGPA(s.GPA); return( 0 ); } Function Definition

17 Structure Element as output Dr. Sadık EşmelioğluCENG 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 */ ,/* no */ 3.7/* GPA */ }; PrtGPA(&s.GPA); return( 0 ); } int main(void) { struct student s = { "Jane", /* fname */ "Doe", /* lname */ ,/* no */ 3.7/* GPA */ }; PrtGPA(&s.GPA); return( 0 ); } Function Definition

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