C Programming

STRUCTURES AND ARRAYS
Structures can also contain arrays.


	struct month {
		int  number_of_days;
		char name[4];
	};

	static struct month this_month = { 31, "Jan" };

	this_month.number_of_days = 31;
	strcpy( this_month.name, "Jan" );
	printf("The month is %s\n", this_month.name );

Note that the array name has an extra element to hold the end of string nul character.


VARIATIONS IN DECLARING STRUCTURES
Consider the following,


	struct date {
	     int month, day, year;
	} todays_date, purchase_date;

or another way is,


	struct date {
	     int month, day, year;
	} todays_date = { 9,25,1985 };

or, how about an array of structures similar to date,


	struct date {
	     int month, day, year;
	} dates[100];

Declaring structures in this way, however, prevents you from using the structure definition later in the program. The structure definition is thus bound to the variable name which follows the right brace of the structures definition.


CLASS EXERCISE C22
Write a program to enter in five dates, Store this information in an array of structures.

Answer


©Copyright B Brown. 1984-1999. All rights reserved.