a tool for grouping elements together.( like an object but just for data types)
Creating a structure
struct struct_name
{
int var1;
int var2;
int var3;
};
Using a structure
variables can now be declared to be a type of "struct_name"
struct struct_name var1
to set the value of the day in the variable :
struct_name.var1 = value1;
to test the value of the variable:
if ( struct_name.var1 == value )
...
example:
struct date { // a structure with 3 parameters
int month;
int day;
int year;
};
struct date today; // a variable of type date
today.month = 5;
today.day = 21;
today.year = 2015;
printf("todays date is %i/%i/%i.\n",today.month, today.day,today.year);
we can also do it this way:
struct date {
int month;
int day;
int year;
} today;
we do not have to give a structure a tag name if all of the variables of a particular structure type are defined when the structure is defined the structure name can be omitted
struct {
int month;
int day;
int year;
} today;
a disadvantage of this is that we can no longer define further instances of the structure in another statement all the variables of this structure type that you want in your program must be defined in the one statement
Initializing structures
struct date today = { 1,5,2019};
just like an array initialization fewer values might be listed than are contained in the structure:
struct date today = { 1,5}; → no value for year
we can do it this way too:
.member = value
i.e:
struct date date1 = {.month = 12, .day = 23};
Compound literals ( C11 only )
we can assign one or more values to a structure in a single statement using what is known as compound literals
today = (struct date){9,12,2016};
Structure arrays
declaring an array of structures is like declaring any other kind of array:
struct date {
int day;
int month;
int year;
};
struct date myDates[10];
myDates[1].month = 6;
myDates[1].day = 5;
myDates[1].year = 2014;
or
struct date myDates[5] = {{12,1,1985},{12,5,1984},{11,12,1998}};
also the inner pairs of braces are optional:
struct date myDates[5] = {12,1,1985,12,5,1984,11,12,1998}; → initialized just the third element of the array to the specified value
struct date myDates[5] = {[2] = {11,12,1998}}; → sets just the month and day of the second element of the myDates array to 12 and 30
struct date myDates[5] = {[2].month = 12 };
example:
struct date {
int day;
int month;
int year;
};
struct date myDates[10] = {[1]=12,2,1998,[2]=19,11,1975,[3]=24,5,1966};
for(int i = 0; i <= 4; ++i)
printf("date: %i/%i/%i\n",myDates[i].day,myDates[i].month,myDates[i].year);
Structures containing arrays
struct month
{
int numberOfDays;
char name[3];
};
this sets up a month structure that contains an integer member called numberOfDays and a character member called name member name is actually an array of three characters
example: an event scheduler with nested structures:
#include <stdio.h>
int main(){
struct date{
int day;
int month;
int year;
};
struct time{
int second;
int minute;
int hour;
};
struct datetime{
struct date sdate;
struct time stime;
};
struct datetime event;
printf("enter a second: ");
scanf("%d",&event.stime.second);
printf("enter a minute: ");
scanf("%d",&event.stime.minute);
printf("enter a hour: ");
scanf("%d",&event.stime.hour);
printf("enter a day: ");
scanf("%d",&event.sdate.day);
printf("enter a month: ");
scanf("%d",&event.sdate.month);
printf("enter a year: ");
scanf("%d",&event.sdate.year);
printf("your even scheduled for: %d/%d/%d %d:%d:%d\n",event.sdate.day,event.sdate.month,event.sdate.year,event.stime.second,event.stime.minute,event.stime.hour);
return 0;
};
struct time
{
struct Date
{
int day;
int month;
int year;
} dob; // a variable
int hour;
int minutes;
int seconds;
};
the declaration is enclosed within the scope of the time structure definition it doesn't exist outside it it becomes impossible to declare a date variable external to the time structure
Structures and pointers
declaring a struct as a pointer
struct date *datePtr;
datePtr = &todaysDate;
(*datePtr).day=21;
using structs as pointers
test the value of month stored in the date struct pointed to by datePtr:
if ((*date*Ptr).month ==12 )
a special operator exists for pointers to structures:
(*x).y
to be more clearly expressed as:
x->y
example:
struct date{
int month;
int day;
int year;
};
struct date today, *datePtr;
datePtr = &today;
datePtr->month = 12;
datePtr->day = 4;
datePtr->year = 1998;
printf("today is : %i/%i/%i\n",datePtr->day,datePtr->month,datePtr->year);