Create a nested structure to track Person's first, middle and last name - C Structure

C examples for Structure:Structure Value

Description

Create a nested structure to track Person's first, middle and last name

Demo Code

#include <stdio.h>
#include <ctype.h>

#define LEN 5//from   w  ww.j  ava2 s .  c  o m
#define MAXNAME 20

struct Name
{
   char first[MAXNAME];
   char middle[MAXNAME];
   char last[MAXNAME];
};

struct Person
{
   int ssn;
   struct Name name;
};

void printpersona(struct Person *); // pass struct by address
void printpersonb(struct Person); // pass struct by value

int main(void)
{
   struct Person people[LEN] = {
      { 1,{ "a", "b", "c" } },
      { 9,{ "d", "e", "f" } },
      { 8,{ "g", "h", "i" } },
      { 2,{ "f", "l" } },
      { 3,{ "f2", "l2" } }
   };

   // part a -- pass by address
   for (int i = 0; i < LEN; i++)
      printpersona(&people[i]);
   puts("");

   // part b -- pass by value
   for (int i = 0; i < LEN; i++)
      printpersonb(people[i]);

   return 0;
}


void printpersona(struct Person *person)
{
   printf("%s, %s ", person->name.last, person->name.first);
   if (person->name.middle[0] != '\0')
      printf("%c. ", toupper(person->name.middle[0]));
   printf("--- %d\n", person->ssn);
}

void printpersonb(struct Person person)
{
   printf("%s, %s ", person.name.last, person.name.first);
   if (person.name.middle[0] != '\0')
      printf("%c. ", toupper(person.name.middle[0]));
   printf("--- %d\n", person.ssn);
}

Result


Related Tutorials