UMBC CS 201, Fall 06
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
** Section: 01XX & 02XX
** EMail: bogar@cs.umbc.edu
** Modified: 9/25/05
** By: Sue Evans
**
** 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
#define LENGTH 30
#define SIZE 3
typedef struct person
{
char name[LENGTH];
int age;
} PERSON;
PERSON GetPerson (void);
void GetPeople (PERSON people[], int numElems);
void PrintPerson (PERSON person);
void PrintPeople (PERSON people[], int numElems);
int main ()
{
PERSON individual;
PERSON people[SIZE];
/* Getting an individual's info */
printf ("Individual's info:\n");
individual = GetPerson ();
/* Printing an individual's info */
printf ("Individual's info:\n");
PrintPerson (individual);
/* 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: PrintPerson()
** Description:
** This function prints all members of
** the PERSON passed into it
** Inputs: the person to be printed
** Output: there is no return value
***************************/
void PrintPerson (PERSON person)
{
printf ("Name: %s\n", person.name);
printf ("Age : %d\n\n", person.age);
}
/************************
** 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: there is no return value
***************************/
void PrintPeople (PERSON people[], int numElems)
{
int i;
for (i = 0; i < numElems; i++)
{
PrintPerson (people[i]);
}
}
The output
linux1[79] % a.out
Individual's info:
Please enter a first name: Tessa
Enter Tessa's age: 26
Individual's info:
Name: Tessa
Age : 26
Fill the array of people:
Info for person #1 :
Please enter a first name: Ben
Enter Ben's age: 39
Info for person #2 :
Please enter a first name: Dawn
Enter Dawn's age: 21
Info for person #3 :
Please enter a first name: Sue
Enter Sue's age: 79
Everyone's info:
Name: Ben
Age : 39
Name: Dawn
Age : 21
Name: Sue
Age : 79
The second person's info:
Name: Dawn
Age : 21
CSEE
|
201
|
201 F'06
|
lectures
|
news
|
help
Tuesday, 22-Aug-2006 07:14:09 EDT