1 / 37

Structs as Function Arguments and Results

Arrays Pass by referance Struts the same way as the basic types in C - char, int, float passed as arguments returned as results. Structs as Function Arguments and Results. typedef struct Person { int age; char gnd; float weight; } PERSON ;. int adjustAge( int oldAge ){

fadhila
Download Presentation

Structs as Function Arguments and Results

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. Arrays • Pass by referance • Struts • the same way as the basic types in C - char, int, float • passed as arguments • returned as results Structs as Function Arguments and Results

  2. typedef struct Person{ int age; char gnd; float weight; • } PERSON;

  3. int adjustAge( int oldAge ){ • if ( oldAge < 39 ) return( ++oldAge ); • else return( oldAge ); • /*return (oldAge<39) ? ++oldage : oldage;*/ • } void main(){ • PERSON Jim; • : • Jim.age = adjustAge( Jim.age ); • : } the use of the members of a struct.

  4. typedef struct Book{ • int edition; • int pages; • double weight; • char type; • int WC; /*weight classification 1 heavy, 0 is not */ • } BOOK; Write a function tack an argument double to test the weight of a book and return 1 if the weight greater than 2 and 0 otherwise. The value to be assign to WC member.

  5. int weightC(double w){ if(w > 2) return 1; else return 0; } void main(){ BOOK myBok; : myBoK.wc = weightC(myBok.weight); : } int weightC(double w){ return (w > 2)? 1 : 0; }

  6. Consider the student struct: add a new member name it GL /*Grades Later*/ Write a function tack a double to test the average of the student and return a later match the average to be assign to the new member Test the function ASS

  7. PERSON get_details() { PERSON temp; printf("Please enter Age, Gender and Weight (Kg): "); scanf("%d %c%f", &temp.age, &temp.gnd, &temp.weight ); return temp; } void main(){ PERSON Jim, Mary, Sid; : Jim = get_details(); : } The whole of a struct is returned by a function

  8. Write a function allow the user to input the details of a BOOK and return it back • typedef struct Book{ • int edition; • int pages; • double weight; • char type; • } BOOK;

  9. BOOK GetBook(){ BOOK tmp; printf(“\nEnter the BOOK information:”); printf(“ [edition, pages, weight, and type]”); scanf(“%d%d%f%c”, &tmp.edition, &tmp.pages. &tmp.weight. &tmp.type); retrun tmp; } void main(){ BOOK myBook; : myBook = GetBook(); : }

  10. Write a function allow the user to input the details of a STUDENTand return it back Test the function ASS

  11. void show_details( PERSONwho ){ • printf( "Person's age was: %d\n", who.age ); • printf( " Gender was: %c\n", who.gnd ); • /* Kg -> lb */ • who.weight = who.weight * 2.2; • printf( " weight (lb) was: %5.1f\n", who.weight ); • } void main(){ PERSON Jim, Mary, Sid; : Jim = get_details(); : show_details(Jim); } Passing a struct as a function argument

  12. Write a function tack a BOOK and print the content out

  13. void PrintBook( const BOOK b){ • printf(“The BOOK information:\n”); • printf(“\n\tedition: %d\n\tpages: %d”, b.edition, b.pages); • printf(“\n\tweight: %f \n\ttype: %c”, b.weight, b.type); • } • void main(){ • BOOK myBook = {2, 120, 2.5, ‘A’}; • : • PrintBook(myBook); • : • }

  14. Write a function tack a STUDENT and print the content out Test the function ASS

  15. to store the same package of information for a number of different entities • typedef struct Person{ int age; char gnd; float weight; • } PERSON; Arrays of structs

  16. PERSON employee[12]; • We have an array of 12 elements • each of which is a structure with 3 members. • We can access any member of any element, employee[9].age = 45; • Also we can treat each element as a whole struct • show_details(employee[5]); • employee[3] = get_details(); Arrays of structs

  17. Consider the following struct BOOK, Create an array of three BOOK s • typedef struct Book{ • int edition; • double weight; • char type; • } BOOK;

  18. BOOK academic[3]; BOOK academic[3] = { {1,2.5,’a’}, {2,3.5,’n’}, {2,1.5,’a’} }; Read in the information of the second

  19. academic[1] = GetBook(); Printout the information of the first

  20. PrintBook(academic[0]); Printout the content of the array

  21. int i; for(i=0; i<3; i++) PrintBook(academic[i]);

  22. 1. Create an array of four STUDNTs with initial values • 2. • Create another array of five STUDNTs • Read in the information of the whole student • Printout the content • Assign the second the information of the second of the array created at 1 • Printout the information of the second ASS

  23. typedef struct{ • int age; • char gnd; • float weight; • char name[20]; • } HUMAN; HUMAN ahm = { 45, ‘M’, 95.1, “Ahmad Ali”}; HUMAN ahm = { 45, ‘M’, 95.1, {‘A’, ‘h’, ‘m’, ‘a’, ‘d’, ‘ ‘, ‘A’, ‘l’, ‘i’, ‘\0’}}; Structs Containing Arrays

  24. char initial = ahm.name[0]; • putchar(initial); • Printout the 3rd char of ahm’s name

  25. char third = ahm.name[2]; printf(“%c”, third); /*putchar(third)*/ allow the user to input the 6th char of ahm’s name

  26. scanf(“%c”, &ahm.name[5]); print the name char by char

  27. int i; for(i=0; i<20; i++) putchar(ahm.name[i]); i=0; while( ahm.name[i] != ‘\0’) putchar(ahm.name[i++]); Print out the name at once

  28. printf(“%s\n”, ahm.name); puts(ahm.name); allow the user to input the ahm’s name

  29. scanf(“%s”, ahm.name); gets(ahm.name); Which is better?

  30. typedef struct Book{ • int edition; • double weight; • char type; • } BOOK; • Rewrite the BOOK with a new member title

  31. typedef struct Book{ • int edition; • double weight; • char type; • char title[30]; • } BOOK; • Create a variable of book with initial values

  32. BOOK myBook = {1,2.5,’a’, “C programming” } change the title to “c how to program”

  33. gets(myBook.title); scanf(“%s”, myBook.title);

  34. Create a Time struct with classification member (“PM”, “AM”) Write a function to print out the content of the Time Create a STUDENT struct with a name member up to 30 char Write two functions to read the information of a student and the second to print out the information. ASS

  35. HUMAN client[2] = { 35, ‘M’, 95.1, “Ahmad Ali”, 25, ‘F’, 50.9, "Salma Zakarea” }; printf( "%s\n", client[1].name ); printf( "%c\n", client[1].name[0] );

  36. HUMAN client[2] = { 35, ‘M’, 95.1, “Ahmad Ali”, 25, ‘F’, 50.9, "Salma Zakarea” }; HUMAN temp; Assign the values of first client to temp • typedef struct{ • int age; • char gnd; • float weight; • char name[20]; • } HUMAN;

  37. temp = client[0]; temp.age = client[0].age; temp.gnd = client[0].gnd; Temp.weight = client[0].weight; strcpy(temp.name, client[0].name);

More Related