Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. //define the structure
  6. struct childInfo {
  7.     char *firstname, *lastname;
  8.     int age;
  9.     char gender; // 'M' of 'F'
  10.     float height, weight;
  11. };
  12.  
  13. //function that displays the contents of childInfo
  14. void display_childInfo(const struct childInfo c);
  15.  
  16. int main()
  17. {
  18.     //create one of the structures
  19.     struct childInfo kid;
  20.  
  21.     //assign values to each of the structure members
  22.     kid.firstname = (char*)malloc(8*sizeof(char)); // 7 + 1 (for the null character)
  23.     if (kid.firstname)
  24.         strcpy(kid.firstname, "William"); // 7 characters
  25.     kid.lastname = (char*)malloc(6*sizeof(char));
  26.     if (kid.firstname)
  27.         strcpy(kid.lastname, "Smith");
  28.     kid.age = 11;
  29.     kid.gender = 'M';
  30.     kid.height = 58.1f;
  31.     kid.weight = 79.3f;
  32.    
  33.     //display the values in each of the members
  34.     display_childInfo(kid);
  35.  
  36.     system("pause");
  37.     return 0;
  38. }
  39.  
  40. // receives a copy of the struct
  41. void display_childInfo(const struct childInfo c)
  42. {
  43.     printf("struct childInfo content:\n\tfirstname: \"%s\"\n", c.firstname);
  44.     printf("\tlastname: \"%s\"\n", c.lastname);
  45.     printf("\tage: %d\n\tgender: '%c'\n", c.age, c.gender);
  46.     printf("\theight: %.1f\n\tweight: %.1f\n\n", c.height, c.weight);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement