Structures

1 / 18

# Structures - PowerPoint PPT Presentation

##### Structures

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Structures Structures and Pointers Structures and Functions

2. Structure Definition • Define a structure book structbook { char title[25]; char author[2][25]; int pages; double price; }; • Define a variable Fiction and intialize structbookFiction = {“A Time to Kill”, {“Arthur Smith”, “John Clark”}, 556, 19.95}; CENG 114

3. Memory Allocation for Fiction &Fiction Fiction.author[0] Fiction.author[1] &Fiction.pages Fiction.title &Fiction.price

4. Accessing Elements using Pointers struct book Fiction; struct book *ptr; ptr = &Fiction; Fiction.title (*ptr).title  ptr->title Fiction.author[0]  (*ptr).author[0]  ptr->author[0] Fiction.price (*ptr).price  ptr->price CENG 114

5. Example • #include <stdio.h> • struct book { • char Title[25]; • char Auth[2][20]; • int Pgs; • double Prc; • }; • typedefstruct book Book_t; • int main(void) { • struct book bk1 = { • "Calculus for Sophmores", • {"John Thompson", "Carrie Thompson"}, • 787, • 89.95 • }; CENG 114

6. Example • 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(""); CENG 114

7. Example • 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); • } CENG 114

8. Example 2 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); } #include <stdio.h> 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 };

9. Example 3 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]); } #include <stdio.h> struct student { intNo; charFname[15], Lname[15]; intQgrds[12]; intQGrd; }; int main(void) { struct student std; struct student *p; inti, j; int temp;

10. Example 3 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); } for(i=0; i<11; i++) { for(j=0; j<11-i; j++) { if(p->Qgrds[j] < p->Qgrds[j+1]) { temp = p->Qgrds[j]; p->Qgrds[j] = p->Qgrds[j+1]; p->Qgrds[j+1] = temp; } } }

11. Structure Inp/Outp in Functions • Assume the following structure struct student { char fname[15]; char lname[15]; int no; double GPA; } ; • Structure as input • Structure as output • Structure as input and output • Structure as output (return) • Structure element as input • Structure element as output • Structure element as output (return) CENG 114

12. Structure as input • Function call • Function Definition • 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); • } CENG 114

13. Structure as output • Function call • Function Definition • 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); } CENG 114

14. Structure as input and output • Function Definition • Function call • void ChngGrd(struct student *std) • { • std->GPA *= (5.0/4.0); • } int main(void) { struct student s; ChngGrd(&s); return(0); } CENG 114

15. Structure as output (return) • Function call • Function Definition • struct studentFillStd2(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); } CENG 114

16. Structure Element as input • Function call • Function Definition • void PrtGPA(doubleGrd) • { • 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); • } CENG 114

17. Structure Element as output • Function call • Function Definition • 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); • } CENG 114

18. Structure Element as output w/Return • Function call • Function Definition • 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); • } CENG 114