UMBC CS 201, Spring 02
Passing arrays of structures to functions
Like any other type of array, it is possible to pass arrays of structure
variables to functions.
Passing an array of structs
For example:
/***************************************
** File: structarray.c
** Author: Sue Bogar
** Date: 10/15/00
** SSN: 123-45-6789
** Section: all
** EMail: bogar@cs.umbc.edu
**
** This program provides an example of
** passing an array of structures to a
** function and passing an individual
** element of an array to a function.
*************************************/
#include
typedef struct person
{
char name[30];
int age;
} PERSON;
#define SIZE 3
PERSON GetPerson (void);
void GetPeople (PERSON people[], int numElems);
void PrintPerson (PERSON person);
void PrintPeople (PERSON people[], int numElems);
int main ()
{
PERSON lee;
PERSON people[SIZE];
/* Getting an individual's info */
printf ("Lee's info:\n");
lee = GetPerson ();
/* Printing an individual's info */
printf ("Lee's info:\n");
PrintPerson (lee);
/* Getting an entire array of
** people's information */
printf("Fill the array of people:\n");
GetPeople (people, SIZE);
/* Printing everyone's info */
printf ("\nEveryone's info:\n");
PrintPeople (people, SIZE);
printf ("\n");
/* Printing an individual's info
** when s/he is an element of the
** array of people */
printf ("The second person's info:\n");
PrintPerson (people[1]);
return 0;
}
/************************
** Function: GetPerson()
** Description:
** This function gets all members of
** the PERSON passed into it by
** prompting the user
** Inputs: none
** Output: the person data is returned
** in a PERSON
***************************/
PERSON GetPerson (void)
{
PERSON p;
printf("Please enter a first name: ");
scanf ("%s", p.name);
printf("Enter %s's age: ", p.name);
scanf ("%d", &p.age);
return p;
}
/************************
** Function: GetPeople()
** Description:
** This function gets the member
** information for each of the
** elements of the array of PERSONs
** passed into it.
** Inputs: the array to be filled and the
** number of elements in that array
** Output: None, but the array is modified
***************************/
void GetPeople (PERSON people[], int numElems)
{
int i;
for (i = 0; i < numElems; i++)
{
printf ("Info for person #%d :\n", i + 1);
people[i] = GetPerson( );
}
}
/************************
** Function: PrintPeople()
** Description:
** This function prints all members of
** each PERSON of the array passed into it
** Inputs: the array of PERSONs and the
** number of elements in that array
** Output: all of the people data is displayed
** there is no return value
***************************/
void PrintPeople (PERSON people[], int numElems)
{
int i;
for (i = 0; i < numElems; i++)
{
PrintPerson (people[i]);
}
}
/************************
** Function: PrintPerson()
** Description:
** This function prints all members of
** the PERSON passed into it
** Inputs: the person to be printed
** Output: the person data is displayed
** there is no return value
***************************/
void PrintPerson (PERSON person)
{
printf ("Name: %s\n", person.name);
printf ("Age : %d\n\n", person.age);
}
The output
linux1[79] % a.out
Lee's info:
Please enter a first name: Lee
Enter Lee's age: 68
Lee's info:
Name: Lee
Age : 68
Fill the array of people:
Info for person #1 :
Please enter a first name: Bob
Enter Bob's age: 44
Info for person #2 :
Please enter a first name: Dennis
Enter Dennis's age: 51
Info for person #3 :
Please enter a first name: Sue
Enter Sue's age: 52
Everyone's info:
Name: Bob
Age : 44
Name: Dennis
Age : 51
Name: Sue
Age : 52
The second person's info:
Name: Dennis
Age : 51
CSEE
|
201
|
201 S'02
|
lectures
|
news
|
help
Thursday, 17-Jan-2002 13:52:13 EST