Advertisement
Guest User

Untitled

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