Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- //define the structure
- struct childInfo {
- char *firstname, *lastname;
- int age;
- char gender; // 'M' of 'F'
- float height, weight;
- };
- //function that displays the contents of childInfo
- void display_childInfo(const struct childInfo c);
- int main()
- {
- //create one of the structures
- struct childInfo kid;
- //assign values to each of the structure members
- kid.firstname = (char*)malloc(8*sizeof(char)); // 7 + 1 (for the null character)
- if (kid.firstname)
- strcpy(kid.firstname, "William"); // 7 characters
- kid.lastname = (char*)malloc(6*sizeof(char));
- if (kid.firstname)
- strcpy(kid.lastname, "Smith");
- kid.age = 11;
- kid.gender = 'M';
- kid.height = 58.1f;
- kid.weight = 79.3f;
- //display the values in each of the members
- display_childInfo(kid);
- system("pause");
- return 0;
- }
- // receives a copy of the struct
- void display_childInfo(const struct childInfo c)
- {
- printf("struct childInfo content:\n\tfirstname: \"%s\"\n", c.firstname);
- printf("\tlastname: \"%s\"\n", c.lastname);
- printf("\tage: %d\n\tgender: '%c'\n", c.age, c.gender);
- printf("\theight: %.1f\n\tweight: %.1f\n\n", c.height, c.weight);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement