0% found this document useful (0 votes)
28 views

Structures in C

Uploaded by

Hoàng Trang My
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Structures in C

Uploaded by

Hoàng Trang My
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

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");

}; for ( i = 0; i < 12; ++i )

const struct month months[12] = printf (" %c%c%c %i\n",

{ { 31, {'J', 'a', 'n'} }, { 28, {'F', 'e', months[i].name[0],


'b'} }, months[i].name[1],
{ 31, {'M', 'a', 'r'} }, { 30, {'A', 'p', months[i].name[2],
'r'} }, months[i].numberOfDays);

{ 31, {'M', 'a', 'y'} }, { 30, {'J', 'u', return 0;


'n'} }, }
Structure variants
struct date{
int month;
int day;
int year;
} todaysDate, purchaseDate;
struct{
int month;
int day;
int year;
} dates[100];
Pointers and structures
struct date{ if ( (*datePtr).month == 12 )
int month;
int day; if ( datePtr->month == 12 )
int year;
};
struct date *datePtr;
datePtr = &todaysDate;
(*datePtr).day = 21;
Example
struct date{
int month;
int day;
int year;
};
struct date today, *datePtr;
datePtr = &today;
datePtr->month = 9;
datePtr->day = 25;
datePtr->year = 2004;
printf ("Today's date is %i/%i/%.2i.\n",
datePtr->month, datePtr->day, datePtr->year % 100);
Example
struct intPtrs{
int *p1;
int *p2;
};
struct intPtrs pointers;
int i1 = 100, i2;
pointers.p1 = &i1;
pointers.p2 = &i2;
*pointers.p2 = -97;
printf ("i1 = %i, *pointers.p1 = %i\n", i1, *pointers.p1);
printf ("i2 = %i, *pointers.p2 = %i\n", i2, *pointers.p2);
Linked List
• The concepts of pointers to structures and structures containing pointers are
very powerful ones in C, for they enable you to create sophisticated data
structures, such as linked lists, doubly linked lists, and trees
• Suppose you define a structure as follows:
struct entry{
int value;
struct entry *next;};
struct entry n1, n2;
n1.next = &n2;
Example
struct entry{ n1.next = &n2;
int value; n2.next = &n3;
struct entry *next; i = n1.next->value;
}; printf ("%i ", i);
struct entry n1, n2, n3; printf ("%i\n", n2.next-
int i; >value);
n1.value = 100;
n2.value = 200;
n3.value = 300;
Returning a Pointer from a Function
struct entry{
int value;
struct entry *next;
};
struct entry *findEntry (struct entry *listPtr, int match)
{
while ( listPtr != (struct entry *) 0 )
if ( listPtr->value == match )
return (listPtr);
else
listPtr = listPtr->next;
return (struct entry *) 0;
}

You might also like