Structures in C
Structures in C
Introduction
• C provides array to group elements of the same type.
• How to group different types not a group?
• C provide a tool named struct (structure) to group elements together.
• For example:
• We want to describe a date: 11/12/2020. We need:
int month = 12;
int day = 11;
int year = 2020;
Structure in C
• We can define a structure called date in C to keep previous example
struct date{
int month;
int day;
int year;
};
struct date today;
today.month = 12;
today.year = 2020
today.day = 11;
Example
int main (void) struct date today;
{ today.month = 9;
today.day = 25;
struct date
today.year = 2004;
{
printf ("Today's date is
int month; %d/%d/%.2d.\n", today.month,
int day; today.day,
int year; today.year % 100);
return 0;
};
}
Example
struct date{ else if(today.month == 12) {
int month;
tomorrow.day = 1;
int day;
tomorrow.month = 1;
int year;
};
tomorrow.year = today.year + 1;
int main (void){ }
struct date today, tomorrow; else {
const int daysPerMonth[12] = { 31, 28, 31, 30, 31, 30, tomorrow.day = 1;
31, 31, 30, 31, 30, 31 };
tomorrow.month = today.month + 1;
printf("Enter today's date (mm dd yyyy): ");
tomorrow.year = today.year;
scanf("%i%i%i", &today.month, &today.day, &today.year);
if( today.day != daysPerMonth[today.month - 1] ) {
}
tomorrow.day = today.day + 1; printf("Tomorrow's date is %i/%i/%.2i.\n",
tomorrow.month = today.month; tomorrow.month,
tomorrow.year = today.year; tomorrow.day, tomorrow.year % 100);
}
return 0;}
Functions and Structures
void printline(void);
void main(){
printline();
printf("This is an example");
printline();
}
void printline(void){
printf("--------------");
printf("\n");
printf("--------------");
}
Example
// Function to find the number of days in a month
int numberOfDays (struct date d)
{
int days;
bool isLeapYear (struct date d);
const int daysPerMonth[12] =
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ( isLeapYear (d) == true && d.month == 2 )
days = 29;
else
days = daysPerMonth[d.month - 1];
return days;
}
Initializing structures
• Initializing structures is similar to initializing arrays
• To initialize the date structure variable:
struct date today = {7, 2, 2005};
struct date today = { .year = 2004 };
• Assign values to structure:
today = (struct date) { 9, 25, 2004 };
today = (struct date) { .month = 9, .day = 25, .year = 2004 };
Arrays of Structures
• C does not limit you to storing simple data types inside an array; it is perfectly
valid to define an array of structures. For example,
struct time experiments[10];
struct date birthdays[15];
Example
struct time for ( i = 0; i < 5; ++i ) {
{ printf ("Time is %.2i:%.2i:%.2i",
int hour; testTimes[i].hour,
int minutes; testTimes[i].minutes,
testTimes[i].seconds);
int seconds;
testTimes[i] = timeUpdate
}; (testTimes[i]);
int main (void) printf (" ...one second later it's
{ %.2i:%.2i:%.2i\n",
struct time timeUpdate (struct time now); testTimes[i].hour,
testTimes[i].minutes, testTimes[i].seconds);
struct time testTimes[5] =
{ { 11, 59, 59 }, { 12, 0, 0 }, {
}
1, 29, 59 }, return 0;
{ 23, 59, 59 }, { 19, 12, 27 }}; }
int i;
Structures containing structures
• We can define a structure that itself contains other structures as one or more of its
members, or you can define structures that contain array
• It is possible to logically group the month, day, and year into a structure called date
and how to group the hour, minutes, and seconds into a structure called time.
struct dateAndTime
{
struct date sdate;
struct time stime;
};
struct dateAndTime event =
{ { 2, 1, 2004 }, { 3, 30, 0 } };
Structures containing arrays
• We want to define a structure called month that contains as its members the
number of days in the month as well as a three-character abbreviation for the month
name
struct month{
int numberOfDays;
char name[3];};
aMonth.numberOfDays = 31;
aMonth.name[0] = 'J';
aMonth.name[1] = 'a';
aMonth.name[2] = 'n’;
struct month aMonth = { 31, { 'J', 'a', 'n' } };
Example
int main (void) { 31, {'J', 'u', 'l'} }, { 31,
{'A', 'u', 'g'} },
{
{ 30, {'S', 'e', 'p'} }, { 31,
int i; {'O', 'c', 't'} },
struct month { 30, {'N', 'o', 'v'} }, { 31,
{ {'D', 'e', 'c'} } };
int numberOfDays; printf ("Month Number of Days\n");
char name[3]; printf ("----- --------------\n");