Structures • A structure combines several data items of different datatypes under one name, unlike an array which holds several data items of the same datatype • A structure is commonly used to define a record, e.g., for a student, using the following attributes: Surname First-name(s) ID number, Student-number Date-of-birth • A structure is a derived datatype created using other datatypes • A structure is a user-defined datatype defined by using the keyword struct 1 1) Defining a structure Before you declare a structure variable, you need to first define its datatype (define a structure) using struct • The format of the struct statement is as follows: struct structure-name // defining a structure, and, thus, creating a datatype { member1 declaration; member2 declaration; ... }; struct keyword • Here is an example: struct Employee { char lastName[20]; char firstName[20]; int age; float salary; }; structure-tag or structure-name Structure elements or members or fields of structure – normal variable declarations 2 • Employee is the name of a structure that holds the details of an employee, namely, last-name, first-name, age and salary, and struct Employee is a datatype • Variables within a structure are called members of the structure • Members of the same structure must have different names, but two structures may have members with similar names 2) Declaring structure variables • Declaring structure variables can be done in two ways: a) Declaring structure variables separately • Structure variables are declared after the structure is defined, as shown in the next slide 3 struct Employee { char lastName[20]; char firstName[20]; int age; float salary; }; //defining a structure /*Separately declaring structure variable, array and pointer */ struct Employee Worker1, Cleaners[5], *EmployeePtr ; • First, a structure, struct Employee, is defined • Separately, a variable, array and pointer, all of type struct Employee (or simply Employee) are declared: Worker1 is a variable of type Employee Cleaners[5] is an array of type Employee *EmployeePtr is a pointer to type Employee 4 b) Declaring structure variables with structure definition struct Employee { char lastName[20]; char firstName[20]; int age; float salary; } Worker1, Cleaners[5], *EmployeePtr; /*Structure variable, array and pointer declared with structure definition */ 5 c) Declaring structure variables globally and locally • A structure can be defined globally, and structure variables separately declared globally or locally in functions, for example: struct Student { char surname[20]; char firstname[20]; char gender[6]; int age; float balance; }; //defining a structure globally struct Student s1; //declaring a structure variable globally int main() { struct Student s2; //declaring a structure variable locally in a function … } 6 • Alternatively, a structure can be defined locally in a function and structure variables declared locally in the function, for example: int main() // main function { struct Student { char surname[20]; char firstname[20]; char gender[6]; int age; float balance; } s1; //declaring variable with structure definition, locally struct Student s2; //separately declaring structure variable, locally … } 7 3) Initialising a structure • A structure can be initialised with data Example 1 struct Student { char surname[20]; char firstname[20]; char gender[6]; int age; float balance; } ; //Initialising a structure struct Stud Std1 = {“Msibi”, “Ray”, “Male”, 20, 2.50}; • • • • • surname contains the string, Msibi firstname contains the string, Ray gender contains the string, Male age contains the integer value, 20 balance contains the floating-point value, 2.50 8 Example 2 struct Point { int x; int y; } Pt1 = {4, 5}; • X contains the value 4 • y contains the value 5 4) Accessing members of a structure a) Using the dot operator • Members of a structure can be accessed using the dot (.) operator • The operator accesses the members via structure variable names 9 i. Using printf() • Members of a structure can be accessed for displaying using printf() • Here is one example: #include <stdio.h> #include <conio.h> int main() { struct Student { char Name[30]; int age; float balance; }; struct Student Std1 = {"Ray Msibi",19,250.60}; printf("Name:\t\t%s\n",Std1.Name); printf("Age:\t\t%d\n",Std1.age); printf("Fees balance:\t%.2f\n",Std1.balance); getch(); return 0; } • The output is: 10 • Here is an another example: #include <stdio.h> #include <conio.h> int main() { struct Student { char Name[30]; int age; float balance; }; struct Student StdArr[] = { {"John Dube",20,500}, {"Jabu Sithole",19,705.70}, {"Mandla Nkosi",21,1004.40} }; int i; for(i=0;i<3;i++) { printf("Student %d\n",i+1); printf("Name:\t\t%s\n",StdArr[i].Name); printf("Age:\t\t%d\n",StdArr[i].age); printf("Fees balance:\t%.2f\n\n",StdArr[i].balance); } getch(); return 0; } 11 • The output is: ii. Reading input data from user at run-time • Members of a structure can be accessed to store data read from the user at run-time (from the standard input) • fgets() can read a string from the standard input • getch() can read a char value from the standard input • scanf() can read int, float and double values from the standard input 12 • Here is an example: #include <stdio.h> #include <conio.h> int main() { struct Student { char Name[30]; char Gender; int Age; float Bal; }; struct Student Std1; printf("Enter name and surname: "); fgets(Std1.Name, sizeof(Std1.Name),stdin); printf("Enter letter for gender: "); Std1.Gender = getch(); ///assigning to Gender member printf("%c\n",Std1.Gender); printf("Enter age: "); scanf("%d",&Std1.Age); printf("Enter balance: "); scanf("%f",&Std1.Bal); printf("\nName and surname:\t%s",Std1.Name); printf("Gender:\t\t\t%c\n",Std1.Gender); printf("Age:\t\t\t%d\n",Std1.Age); printf("Fees balance:\t\t%.2f\n",Std1.Bal); getch(); return 0; } 13 • The output is: iii. Assigning to members of a structure • Members of a structure can be accessed to assign data for storage • A string can only be assigned when declaring a structure • Values of other datatypes (int, float, char, double) can be assigned after a structure is declared 14 • Here is an example: #include <stdio.h> #include <conio.h> int main() { struct Student { char Name[30]; char Gender; int Age; float Bal; }; struct Student Std1 = {"Roy Gumede"}; Std1.Gender = 'M'; Std1.Age = 20; Std1.Bal = 400; printf("Name and surname:\t%s\n",Std1.Name); printf("Gender:\t\t\t%c\n",Std1.Gender); printf("Age:\t\t\t%d\n",Std1.Age); printf("Fees balance:\t\t%.2f\n",Std1.Bal); getch(); return 0; } • The output is: 15