Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.

Similar presentations


Presentation on theme: "Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except."— Presentation transcript:

1 Structs

2 Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create a structure called person which is made up of a string for the name and an integer for the age.

3 Person struct Here is how you would create that person structure in C: struct person { char name[30]; int age; };

4 Creating a variable of struct type The above is just a declaration of a type. You must still create a variable of that type to be able to use it. Here is how you create a variable called p of the type person:

5 Program #include struct person { char name[30]; int age; }; main() { struct person p; }

6 Accessing the elements of a structure To access the string or integer of the structure you must use a dot between the structure name and the variable name. #include struct person { char name[30]; int age; }; main() { struct person p; p.name = "John Smith"; p.age = 25; printf("%s",p.name); printf("%d",p.age); }

7 Struct format The format for defining a structure is struct Tag { Members }; Where Tag is the name of the entire type of structure and Members are the variables within the struct. e.g. struct example { int x; }; example is the Tag, and x is a member.

8 To actually create a single structure the syntax is struct Tag name_of_single_structure; struct example an_example; an_example is an instance of struct example

9 To access a variable of the structure it goes name_of_single_structure.name_of_variable; E.g. an_example.x = 33;

10 Putting it all together //declare the struct struct example { int x; }; Create variables of type struct example struct example an_example; //Treating it like a normal variable type// // Access its members an_example.x = 33;

11 Another example Here is an example program:

12 struct database { int id_number; int age; float salary; }; main() { database employee; //There is now an employee variable that has modifiable variables inside it. employee.age = 22; employee.id_number = 1; employee.salary = 12000.21; }

13 Employee Record ID_numberAgeSalary 12212000.10

14 Type definitions You can give your own name to a variable using a type definition. Here is an example of how to create a type definition called intptr for a pointer to an integer. #include typedef int *intptr; int main() { intptr ip; return 0; } Type definitions for a structure If you don't like to use the word struct when declaring a structure variable then you can create a type definition for the structure. The name of the type definition of a structure is usually all in uppercase letters. #include typedef struct person { char name[30]; int age; } PERSON; int main() { PERSON p; p.name = "John Smith"; p.age = 25; printf("%s",p.name); printf("%d",p.age); return 0; }

15 Exercise Create a person struct with fields Name, Address, Student no. Grade. Create 2 variables of type person Assign values to each of their fields. Print out who has the highest grade.

16 #include struct person {char name[40]; char address[80]; char student_no[10]; int grade; }

17 main() { person p1,p2; p1.name = “fred” p1.address = “kevin st” p1.student_no = “c102445” p1.grade = 72;

18 p2.name = “mary” p2.address = “bolton st” p2.student_no = “b10775” p2.grade = 87; if (p1.grade > p2.grade) {printf(“fred’s mark %d is greater than mary’s %d”, p1.grade,p2.grade);} else {printf(“mary’s mark %d is greater than fred’s %d”, p2.grade,p1.grade);} }

19 Exercise Create a struct which represents a book.

20 struct book struct book { chartitle; char author; printf("enter for book %d",i +1); gets(collection[i]. publisher; char isbn; int year; char genre; char illustrator; }

21 Can have an array of these structs e.g. catalog is an array of 100 books struct book catalog[100];

22 Exercise 2 Using book struct create a list of 5 books and read in their details

23 #include struct book { char title[40]; char author[40]; char publisher[40]; char isbn[40]; int year; char genre[40]; char illustrator[40]; }

24 main() { struct book collection[5]; int i; for(i = 0; i < 5,i++) { printf("enter title for book %d",i +1); gets(collection[i].title); printf("enter author for book %d",i +1); gets(collection[i].author); printf("enter publisher for book %d",i +1); gets(collection[i]. publisher); printf("enter year for book %d",i +1); scanf(“%d”, collection[i].year); printf("enter isbn for book %d",i +1); gets(collection[i]. isbn); printf("enter genre for book %d",i +1); gets(collection[i]. genre; printf("enter illustrator for book %d",i +1); gets(collection[i]. illustrator); }

25 Note We can also define pointers to structs struct book *book1;


Download ppt "Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except."

Similar presentations


Ads by Google