180 likes | 244 Views
Structures. Definition and Initialization. Structure. Arrays are limited to same type of elements (only int or only char or only double). Structures group different types of variable together A book can have the following information: Title (string – a char array)
E N D
Structures Definition and Initialization
Structure • Arrays are limited to same type of elements (only int or only char or only double). • Structures group different types of variable together • A book can have the following information: • Title (string – a char array) • Author (string array – a 2D array) • Number of pages (int) • Price (double) CENG 114
Structure Definition struct book { char title[25]; char author[2][25]; int pages; double price; }; • Here book is not a variable, it is a variable type • when a structure is defined, it does not require memory • memory is allocated only when a variable of struct type is defined CENG 114
Defining a Structure variable struct bookFiction; • book is the variable type • Fiction is the variable • How many bytes are required for Fiction char title[25]; 25 bytes char author[2][25]; 50 bytes int pages; 4 bytes double price; 8 bytes 87 CENG 114
Memory Allocation for Fiction &Fiction Fiction.author[0] Fiction.author[1] &Fiction.pages Fiction.title &Fiction.price
Structure Initialization struct bookFiction = {“A Time to Kill”, {“Arthur Smith”, “John Clark”}, 556, 19.95}; CENG 114
Memory Allocation for Fiction &Fiction Fiction.author[0] Fiction.author[1] &Fiction.pages Fiction.title &Fiction.price
Accessing Elements struct bookFiction = {“A Time to Joy”, {“Arthur Smith”, “John Clark”}, 556, 19.95}; Fiction.title string size 24+1 Fiction.author[0] string array – 2 strings x 24+1 Fiction.author[1] Fiction.pages integer Fiction.price double CENG 114
Example #include <stdio.h> struct book { char title[25]; char author[2][25]; int pages; double price; }; int main(void) { struct book Fiction = { "A Time to Joy", {"Arthur Smith", "John Clark"}, 556, 19.95 }; printf("%s\n", Fiction.title); printf("%s & %s\n", Fiction.author[0], Fiction.author[1]); printf("%d\n", Fiction.pages); printf("%.2f\n", Fiction.price); return(0); }
Example 2 #include <stdio.h> struct book { char title[25]; char author[2][25]; int pages; double price; }; int main(void) { struct book Fiction ; • printf("Enter title: "); gets(Fiction.title); • printf("Enter author 1: "); gets(Fiction.author[0]); • printf("Enter author 2: "); gets(Fiction.author[1]); • printf("Enter pages: "); scanf("%d", &Fiction.pages); • printf("Enter price: "); scanf("%lf", &Fiction.price); printf("%s\n", Fiction.title); printf("%s & %s\n", Fiction.author[0], Fiction.author[1]); printf("%d\n", Fiction.pages); printf("%.2f\n", Fiction.price); return(0); }
Example 3 • Write a program that will read a student’s Number, First Name, Last Name, and 12 quiz grades each out of 10. Then it will calculate the total quiz grade by adding 10 highest grades. All student data will be kept in a structure. At the end the program will print all the student data to the screen
Example 3 printf("Enter std no: "); scanf("%d", &std.No); fflush(stdin); printf("Enter std first name: "); gets(std.Fname); fflush(stdin); printf("Enter std last name: "); gets(std.Lname); for(i=0; i<12; i++) { printf("Enter quiz grade %2d: ", i+1); scanf("%d", &std.Qgrds[i]); } #include <stdio.h> struct student { intNo; charFname[15], Lname[15]; intQgrds[12]; intQGrd; }; int main(void) { struct student std; inti, j; int temp;
Example 3 std.QGrd = 0; for(i=0; i<10; i++) std.QGrd += std.Qgrds[i]; printf("%-12d", std.No); printf("%-15s", std.Fname); printf("%-15s", std.Lname); printf("%3d\n", std.QGrd); return(0); } for(i=0; i<11; i++) { for(j=0; j<11-i; j++) { if(std.Qgrds[j] < std.Qgrds[j+1]) { temp = std.Qgrds[j]; std.Qgrds[j] = std.Qgrds[j+1]; std.Qgrds[j+1] = temp; } } }
Nested Structures • A structure definition can contain other structures struct date{ int day; char month[10]; int year; }; struct customer{ int No; char Name[2][15]; struct date bday; };
Example 4 • Check to see if today is the customers birthday print a reminder on the screen.
Example 4 • int main(void) { • struct customer c = { 1545, {"Nuri", "Kaynarca"}, • {30, "Nisan", 1990}}; • struct date today = {29, "Nisan", 2014}; • if(c.bday.day== today.day && strcmp(c.bday.month, today.month) == 0) • printf("Today is %s %s's birthday!\n", c.Name[0], c.Name[1]); • else • printf("Today is NOT %s %s's birthday!\n", c.Name[0], c.Name[1]); • return(0); • } #include <stdio.h> #include <string.h> struct date{ int day; char month[10]; int year; }; struct customer{ intNo; charName[2][15]; structdatebday; };
structure definition using typedef • typedef can be used after struct definition: struct date { int Day; /* 0-31 */ int Month; /* 1-12 */ int Year; /* 4 digit year */ }; • Now you can define a type for date: typedefstruct date Date_t; • Date_t can be usedto define variables just like int, double, char, … struct date bday Date_tbday;
structure definition using typedef • Another way to define a structure is: typedefstruct { int Day; /* 0-31 */ int Month; /* 1-12 */ int Year; /* 4 digit year */ } Date_t; • Now you do not need to use the word struct to define variables: Date_tbday;