1 / 37

Structures of Functions

Structures of Functions. Group 3 - Steven Lyons - Jonathan Velez - Christopher Wallace Graciela Alonso Alert Kusaku. Today we’ll be discussing…. Passing a structure to a function without and with a pointer Summary of the lessons learned. 2. Program Diagram. CREATE STRUCTURE DATA.

lenci
Télécharger la présentation

Structures of Functions

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structures of Functions • Group 3- Steven Lyons • - Jonathan Velez • - Christopher Wallace • Graciela Alonso • Alert Kusaku

  2. Today we’ll be discussing… • Passing a structure to a function without and with a pointer • Summary of the lessons learned 2

  3. Program Diagram CREATE STRUCTURE DATA DECLARE A FUNCTION PROTOTYPE SPECIFY STRUCTURE DATA VARIABLE“REC” RETRIEVE USER INPUT PURPOSE OF THE PROGRAM: THIS PROGRAM ALLOWS THE USER TO INPUT INFORMATION REGARDING A DONER’S NAME AND THE AMOUNT DONATED INTO MEMORY. IT ALSO HAS THE CAPABILITY OF PRINTING OUT THE INFORMATION. CALL THE FUNCTION PRINT OUT RESULTS

  4. The Code • /* Demonstrates passing a structure to a function. */ • #include <stdio.h> • /* Declare and define a structure to hold the data. */ • struct data{ • float amount; • char fname[30]; • char lname[30]; • }; • /* The function prototype. The function has no return value, */ • /* and it takes a structure of type data as its one argument. */ • void print_rec(struct data x); • int main(void) • { • struct data rec; • /* Input the data from the keyboard. */ • printf("Enter the donor's first and last names,\n"); • printf("separated by a space: "); • scanf("%s %s", rec.fname, rec.lname); • printf("\nEnter the donation amount: "); • scanf("%f", &rec.amount); • /* Call the display function. */ • print_rec( rec ); • return 0; • } • void print_rec(struct data x) • { • printf("\nDonor %s %s gave $%.2f.", x.fname, x.lname, • x.amount); • }

  5. /* Demonstrates passing a structure to a function. */ #include <stdio.h> /* Declare and define a structure to hold the data. */ struct data{ float amount; char fname[30]; char lname[30]; }; /* The function prototype. The function has no return value, */ /* and it takes a structure of type data as its one argument. */ void print_rec(struct data x); int main(void) { struct data rec; /* Input the data from the keyboard. */ printf("Enter the donor's first and last names,\n"); printf("separated by a space: "); scanf("%s %s", rec.fname, rec.lname); printf("\nEnter the donation amount: "); scanf("%f", &rec.amount); /* Call the display function. */ print_rec( rec ); return 0; } void print_rec(struct data x) { printf("\nDonor %s %s gave $%.2f.", x.fname, x.lname, x.amount); } Headers • /* Demonstrates passing a structure to a function. */ • #include <stdio.h> • /* Declare and define a structure to hold the data. */

  6. /* Demonstrates passing a structure to a function. */ #include <stdio.h> /* Declare and define a structure to hold the data. */ struct data{ float amount; char fname[30]; char lname[30]; }; /* The function prototype. The function has no return value, */ /* and it takes a structure of type data as its one argument. */ void print_rec(struct data x); int main(void) { struct data rec; /* Input the data from the keyboard. */ printf("Enter the donor's first and last names,\n"); printf("separated by a space: "); scanf("%s %s", rec.fname, rec.lname); printf("\nEnter the donation amount: "); scanf("%f", &rec.amount); /* Call the display function. */ print_rec( rec ); return 0; } void print_rec(struct data x) { printf("\nDonor %s %s gave $%.2f.", x.fname, x.lname, x.amount); } Defining a Structure • struct data{ • float amount; • char fname[30]; • char lname[30]; • };Semicolon needed to end structure data

  7. /* Demonstrates passing a structure to a function. */ #include <stdio.h> /* Declare and define a structure to hold the data. */ struct data{ float amount; char fname[30]; char lname[30]; }; /* The function prototype. The function has no return value, */ /* and it takes a structure of type data as its one argument. */ void print_rec(struct data x); int main(void) { struct data rec; /* Input the data from the keyboard. */ printf("Enter the donor's first and last names,\n"); printf("separated by a space: "); scanf("%s %s", rec.fname, rec.lname); printf("\nEnter the donation amount: "); scanf("%f", &rec.amount); /* Call the display function. */ print_rec( rec ); return 0; } void print_rec(struct data x) { printf("\nDonor %s %s gave $%.2f.", x.fname, x.lname, x.amount); } Function Prototype • void print_rec(struct data x);

  8. /* Demonstrates passing a structure to a function. */ #include <stdio.h> /* Declare and define a structure to hold the data. */ struct data{ float amount; char fname[30]; char lname[30]; }; /* The function prototype. The function has no return value, */ /* and it takes a structure of type data as its one argument. */ void print_rec(struct data x); int main(void) { struct data rec; /* Input the data from the keyboard. */ printf("Enter the donor's first and last names,\n"); printf("separated by a space: "); scanf("%s %s", rec.fname, rec.lname); printf("\nEnter the donation amount: "); scanf("%f", &rec.amount); /* Call the display function. */ print_rec( rec ); return 0; } void print_rec(struct data x) { printf("\nDonor %s %s gave $%.2f.", x.fname, x.lname, x.amount); } Main Function • int main(void) • { • struct data rec; • /* Input the data from the keyboard. */ • printf("Enter the donor's first and last names,\n"); • printf("separated by a space: "); • scanf("%s %s", rec.fname, rec.lname); • printf("\nEnter the donation amount: "); • scanf("%f", &rec.amount); • /* Call the display function. */ • print_rec( rec ); • return 0; • }

  9. /* Demonstrates passing a structure to a function. */ #include <stdio.h> /* Declare and define a structure to hold the data. */ struct data{ float amount; char fname[30]; char lname[30]; }; /* The function prototype. The function has no return value, */ /* and it takes a structure of type data as its one argument. */ void print_rec(struct data x); int main(void) { struct data rec; /* Input the data from the keyboard. */ printf("Enter the donor's first and last names,\n"); printf("separated by a space: "); scanf("%s %s", rec.fname, rec.lname); printf("\nEnter the donation amount: "); scanf("%f", &rec.amount); /* Call the display function. */ print_rec( rec ); return 0; } void print_rec(struct data x) { printf("\nDonor %s %s gave $%.2f.", x.fname, x.lname, x.amount); } PRINTF/ SCANF • int main(void) • { • struct data rec; • /* Input the data from the keyboard. */ • printf("Enter the donor's first and last names,\n"); • printf("separated by a space: "); • scanf("%s %s", &rec.fname, &rec.lname); • printf("\nEnter the donation amount: "); • scanf("%f", &rec.amount); • /* Call the display function. */ • print_rec( rec ); • return 0; • }

  10. /* Demonstrates passing a structure to a function. */ #include <stdio.h> /* Declare and define a structure to hold the data. */ struct data{ float amount; char fname[30]; char lname[30]; }; /* The function prototype. The function has no return value, */ /* and it takes a structure of type data as its one argument. */ void print_rec(struct data x); int main(void) { struct data rec; /* Input the data from the keyboard. */ printf("Enter the donor's first and last names,\n"); printf("separated by a space: "); scanf("%s %s", rec.fname, rec.lname); printf("\nEnter the donation amount: "); scanf("%f", &rec.amount); /* Call the display function. */ print_rec( rec ); return 0; } void print_rec(struct data x) { printf("\nDonor %s %s gave $%.2f.", x.fname, x.lname, x.amount); } CALLING THE FUNCTION • int main(void) • { • struct data rec; • /* Input the data from the keyboard. */ • printf("Enter the donor's first and last names,\n"); • printf("separated by a space: "); • scanf("%s %s", rec.fname, rec.lname); • printf("\nEnter the donation amount: "); • scanf("%f", &rec.amount); • /* Call the display function. */ • print_rec( rec ); • return 0; • }

  11. /* Demonstrates passing a structure to a function. */ #include <stdio.h> /* Declare and define a structure to hold the data. */ struct data{ float amount; char fname[30]; char lname[30]; }; /* The function prototype. The function has no return value, */ /* and it takes a structure of type data as its one argument. */ void print_rec(struct data x); int main(void) { struct data rec; /* Input the data from the keyboard. */ printf("Enter the donor's first and last names,\n"); printf("separated by a space: "); scanf("%s %s", rec.fname, rec.lname); printf("\nEnter the donation amount: "); scanf("%f", &rec.amount); /* Call the display function. */ print_rec( rec ); return 0; } void print_rec(struct data x) { printf("\nDonor %s %s gave $%.2f.", x.fname, x.lname, x.amount); } Print_rec Function Definition • void print_rec(struct data x) • { • printf("\nDonor %s %s gave $%.2f.", x.fname, x.lname, • x.amount); • }

  12. The Code Using a *Pointer • /* Demonstrates passing a structure to a function. */ • #include <stdio.h> • /* Declare and define a structure to hold the data. */ • struct data{ • float amount; • char fname[30]; • char lname[30]; }; • /* The function prototype. The function has no return value, */ /* and it takes a structure of type data as its one argument. */ • void print_rec(struct data* x); • int main(void) • { data *rec = new data; • /* Input the data from the keyboard. */ • printf("Enter the donor's first and last names,\n"); • printf("separated by a space: "); • scanf("%s %s", rec->fname, rec->lname); • printf("\nEnter the donation amount: "); • scanf("%f", &rec->amount); • /* Call the display function. */ • print_rec( rec ); return 0; } • void print_rec(struct data * x) • { printf("\nDonor %s %s gave $%.2f.", x->fname, x->lname, x->amount); }

  13. Defining a Structure1. struct data{ float amount; char fname[30]; char lname[30]; }; • /* Demonstrates passing a structure to a function. */ • #include <stdio.h> • /* Declare and define a structure to hold the data. */ • struct data{ • float amount; • char fname[30]; • char lname[30]; }; • /* The function prototype. The function has no return value, */ /* and it takes a structure of type data as its one argument. */ • void print_rec(struct data* x); • int main(void) • { data *rec = new data; • /* Input the data from the keyboard. */ • printf("Enter the donor's first and last names,\n"); • printf("separated by a space: "); • scanf("%s %s", rec->fname, rec->lname); • printf("\nEnter the donation amount: "); • scanf("%f", &rec->amount); • /* Call the display function. */ • print_rec( rec ); return 0; } • void print_rec(struct data * x) • { printf("\nDonor %s %s gave $%.2f.", x->fname, x->lname, x->amount); }

  14. Function Prototype10. void print_rec(struct data* x); • /* Demonstrates passing a structure to a function. */ • #include <stdio.h> • /* Declare and define a structure to hold the data. */ • struct data{ • float amount; • char fname[30]; • char lname[30]; }; • /* The function prototype. The function has no return value, */ /* and it takes a structure of type data as its one argument. */ • void print_rec(structdata* x); • int main(void) • { data *rec = new data; • /* Input the data from the keyboard. */ • printf("Enter the donor's first and last names,\n"); • printf("separated by a space: "); • scanf("%s %s", rec->fname, rec->lname); • printf("\nEnter the donation amount: "); • scanf("%f", &rec->amount); • /* Call the display function. */ • print_rec( rec ); return 0; } • void print_rec(struct data * x) • { printf("\nDonor %s %s gave $%.2f.", x->fname, x->lname, x->amount); }

  15. Main Function11.int main(void)12. { data *rec = new data; 13./* Input the data from the keyboard. */ 14. printf("Enter the donor's first and last names,\n"); 15. printf("separated by a space: "); 16. scanf("%s %s", rec->fname,rec->lname); 17 printf("\nEnter the donation amount: "); 18. scanf("%f", &rec->amount); 19. /* Call the display function. */ 20. print_rec( rec ); return 0; } • /* Demonstrates passing a structure to a function. */ • #include <stdio.h> • /* Declare and define a structure to hold the data. */ • struct data{ • float amount; • char fname[30]; • char lname[30]; }; • /* The function prototype. The function has no return value, */ /* and it takes a structure of type data as its one argument. */ • void print_rec(struct data* x); • int main(void) • { data *rec = new data; • /* Input the data from the keyboard. */ • printf("Enter the donor's first and last names,\n"); • printf("separated by a space: "); • scanf("%s %s", rec->fname, rec->lname); • printf("\nEnter the donation amount: "); • scanf("%f", &rec->amount); • /* Call the display function. */ • print_rec( rec ); return 0; } • void print_rec(struct data * x) • { printf("\nDonor %s %s gave $%.2f.", x->fname, x->lname, x->amount); }

  16. Print_rec Function20. print_rec( rec ); • /* Demonstrates passing a structure to a function. */ • #include <stdio.h> • /* Declare and define a structure to hold the data. */ • struct data{ • float amount; • char fname[30]; • char lname[30]; }; • /* The function prototype. The function has no return value, */ /* and it takes a structure of type data as its one argument. */ • void print_rec(struct data* x); • int main(void) • { data *rec = new data; • /* Input the data from the keyboard. */ • printf("Enter the donor's first and last names,\n"); • printf("separated by a space: "); • scanf("%s %s", rec->fname, rec->lname); • printf("\nEnter the donation amount: "); • scanf("%f", &rec->amount); • /* Call the display function. */ • print_rec( rec ); return 0; } • void print_rec(struct data * x) • { printf("\nDonor %s %s gave $%.2f.", x->fname, x->lname, x->amount); }

  17. Print_rec Function *Defined21.void print_rec(struct data * x) 22. { printf("\nDonor %s %s gave $%.2f.", x->fname, x->lname, x->amount); } • /* Demonstrates passing a structure to a function. */ • #include <stdio.h> • /* Declare and define a structure to hold the data. */ • struct data{ • float amount; • char fname[30]; • char lname[30]; }; • /* The function prototype. The function has no return value, */ /* and it takes a structure of type data as its one argument. */ • void print_rec(struct data* x); • int main(void) • { data *rec = new data; • /* Input the data from the keyboard. */ • printf("Enter the donor's first and last names,\n"); • printf("separated by a space: "); • scanf("%s %s", rec->fname, rec->lname); • printf("\nEnter the donation amount: "); • scanf("%f", &rec->amount); • /* Call the display function. */ • print_rec( rec ); return 0; } • void print_rec(struct data * x) • { printf("\nDonor %s %s gave $%.2f.", x->fname, x->lname, x->amount); }

  18. External Example: • Program Diagram • Code • Code using pointers • Output http://publications.gbdirect.co.uk/c_book/chapter6/structures.html

  19. Program Diagram SPECIFY STRUCTURE ARRAY “AR” RETRIEVE USER INPUT CREATE STRUCTURE DATA DECLARE A FUNCTION PROTOTYPE CALL THE FUNCTION INFUN PURPOSE OF THE PROGRAM: THIS PROGRAM ALLOWS THE USER TO INPUT LETTERS, ASSIGNS IT A VALUE FOR FONT TYPE AND SIZE. IT ALSO PRINTS OUT THE LETTERS AND THEIR PROPERTIES IN ALPHABETICAL ORDER. MAKE WP_CHAR STRUCTURES PRINT OUT RESULTS PUT STRUCTURES IN ORDER

  20. #include <stdio.h> • #include <stdlib.h> • #define ARSIZE 10 • struct wp_char{ • char wp_cval; • short wp_font; • short wp_psize; • }ar[ARSIZE]; • /* • * type of the input function - • * could equally have been declared above; • * it returns a structure and takes no arguments. • */ • struct wp_charinfun(void); • main(){ • inticount, lo_indx, hi_indx; • for(icount = 0; icount < ARSIZE; icount++){ • ar[icount] = infun(); • if(ar[icount].wp_cval == '\n'){ • /* • * Leave the loop. • * not incrementing icount means that the • * '\n' is ignored in the sort • */ • break; • } • } • /* now a simple exchange sort */ • for(lo_indx = 0; lo_indx <= icount-2; lo_indx++) • for(hi_indx = lo_indx+1; hi_indx <= icount-1; hi_indx++){ • if(ar[lo_indx].wp_cval > ar[hi_indx].wp_cval){ • /* • * Swap the two structures. • */ • struct wp_charwp_tmp = ar[lo_indx]; • ar[lo_indx] = ar[hi_indx]; • ar[hi_indx] = wp_tmp; • } • } • /* now print */ • for(lo_indx = 0; lo_indx < icount; lo_indx++){ • printf("%c %d %d\n", ar[lo_indx].wp_cval, • ar[lo_indx].wp_font, • ar[lo_indx].wp_psize); • } • exit(EXIT_SUCCESS); • } • struct wp_char • infun(void){ • struct wp_charwp_char; • wp_char.wp_cval = getchar(); • wp_char.wp_font = 2; • wp_char.wp_psize = 10; • return(wp_char); • } The Code • The Header • Structure • Function Prototype • Main Function • Function Definition

  21. The Headers • Includes the stdio library • Includes the stdlib library • Defines ARSIZE as 10 • #include <stdio.h> • #include <stdlib.h> • #define ARSIZE 10

  22. Structure • Structure definition • wp_cval • wp_font • wp_psize • Array • comment • struct wp_char{ • char wp_cval; • short wp_font; • short wp_psize; • }ar[ARSIZE]; • /* • * type of the input function - • * could equally have been declared above; • * it returns a structure and takes no arguments. • */

  23. Function Prototype • Function infun: • Takes in no variables • Returns a wp_char structure • struct wp_charinfun(void);

  24. Main Function • Opening of main • Declare variables • for loop to run up to 10 times (ARSIZE) • The structures returned from the infun function go into ar array. • If the structure is a new line character, exit loop. • main(){ • inticount, lo_indx, hi_indx; • for(icount = 0; icount < ARSIZE; icount++){ • ar[icount] = infun(); • if(ar[icount].wp_cval == '\n'){ • /* • * Leave the loop. • * not incrementing icount means that the • * '\n' is ignored in the sort • */ • break; • } • }

  25. /* now a simple exchange sort */ • for(lo_indx = 0; lo_indx <= icount-2; lo_indx++) • for(hi_indx = lo_indx+1; hi_indx <= icount-1; hi_indx++){ • if(ar[lo_indx].wp_cval > ar[hi_indx].wp_cval){ • /* • * Swap the two structures. • */ • struct wp_charwp_tmp = ar[lo_indx]; • ar[lo_indx] = ar[hi_indx]; • ar[hi_indx] = wp_tmp; • } • } • /* now print */ • for(lo_indx = 0; lo_indx < icount; lo_indx++){ • printf("%c %d %d\n", ar[lo_indx].wp_cval, • ar[lo_indx].wp_font, • ar[lo_indx].wp_psize); • } • exit(EXIT_SUCCESS); • } • for loop puts structures in alphabetical order. • If current character value is greater, switch with lower structure. • for loop prints out values of structures in the structure array.

  26. Function • Header of function • Makes wp_char structure wp_char. • wp_cval get’s value of user’s input (one character) • wp_font get’s value of 2 • wp_psize get’s value of 10 • Returns wp_char • struct wp_char • infun(void){ • struct wp_charwp_char; • wp_char.wp_cval = getchar(); • wp_char.wp_font = 2; • wp_char.wp_psize = 10; • return(wp_char); • }

  27. #include <stdio.h> • #include <stdlib.h> • #define ARSIZE 10 • struct wp_char{ • char wp_cval; • short wp_font; • short wp_psize; • }ar[ARSIZE]; • void infun(struct wp_char *); • main(){ • struct wp_charwp_tmp, *lo_indx, *hi_indx, *in_p; • for(in_p = ar; in_p < &ar[ARSIZE]; in_p++){ • infun(in_p); • if(in_p->wp_cval == '\n'){ • /* • * Leave the loop. • * not incrementing in_p means that the • * '\n' is ignored in the sort • */ • break; • } • } • /* • * Now a simple exchange sort. • * We must be careful to avoid the danger of pointer underflow, • * so check that there are at least two entries to sort. • */ • if(in_p-ar > 1) for(lo_indx = ar; lo_indx <= in_p-2; lo_indx++){ • for(hi_indx = lo_indx+1; hi_indx <= in_p-1; hi_indx++){ • if(lo_indx->wp_cval > hi_indx->wp_cval){ • /* • * Swap the structures. • */ • struct wp_charwp_tmp = *lo_indx; • *lo_indx = *hi_indx; • *hi_indx = wp_tmp; • } • } • } • /* now print */ • for(lo_indx = ar; lo_indx < in_p; lo_indx++){ • printf("%c %d %d\n", lo_indx->wp_cval, • lo_indx->wp_font, • lo_indx->wp_psize); • } • exit(EXIT_SUCCESS); • } • void • infun( struct wp_char *inp){ • inp->wp_cval = getchar(); • inp->wp_font = 2; • inp->wp_psize = 10; • return; • } The Code using Pointers • The Header • Structure • Function Prototype • Main Function • Function Definition

  28. The Headers (The same as other) • Includes the stdio library • Includes the stdlib library • Defines ARSIZE as 10 • #include <stdio.h> • #include <stdlib.h> • #define ARSIZE 10

  29. Structure (The same as other) • Structure definition • wp_cval • wp_font • wp_psize • Array • comment • struct wp_char{ • char wp_cval; • short wp_font; • short wp_psize; • }ar[ARSIZE];

  30. Function Prototype • Function infun: • Uses pointer to wp_char structure • Returns nothing • void infun(struct wp_char *);

  31. Main Function • main(){ • struct wp_charwp_tmp, *lo_indx, *hi_indx, *in_p; • for(in_p = ar; in_p < &ar[ARSIZE]; in_p++){ • infun(in_p); • if(in_p->wp_cval == '\n'){ • /* • * Leave the loop. • * not incrementing in_p means that the • * '\n' is ignored in the sort • */ • break; • } • } • Opening of main • Declare variables • for loop to run up to 10 times (ARSIZE) • The infun function uses the in_p pointer, which points to a wp_char array. • If the structure is a new line character, exit loop.

  32. if(in_p-ar > 1) for(lo_indx = ar; lo_indx <= in_p-2; lo_indx++){ • for(hi_indx = lo_indx+1; hi_indx <= in_p-1; hi_indx++){ • if(lo_indx->wp_cval > hi_indx->wp_cval){ • /* • * Swap the structures. • */ • struct wp_charwp_tmp = *lo_indx; • *lo_indx = *hi_indx; • *hi_indx = wp_tmp; • } • } • } • Checks that there is more than one structure. If yes, does for loop to put in alphabetical order. • If current character value is greater, switch with lower structure.

  33. Using a for loop prints out all structures in alphabetical order. • Successful exit. • /* now print */ • for(lo_indx = ar; lo_indx < in_p; lo_indx++){ • printf("%c %d %d\n", lo_indx->wp_cval, • lo_indx->wp_font, • lo_indx->wp_psize); • } • exit(EXIT_SUCCESS); • }

  34. Function • Header of function • Takes wp_char structure pointer inp. • wp_cval get’s value of user’s input (one character) • wp_font get’s value of 2 • wp_psize get’s value of 10 • Returns to main. • void • infun( struct wp_char *inp){ • inp->wp_cval = getchar(); • inp->wp_font = 2; • inp->wp_psize = 10; • return; • }

  35. Lessons Learned • Structure values can be easily passed over with a "=" operator • That there can be arrays of structures • How to use structures • Passing structures to functions • Using the dot operator • Using the arrow operator

More Related