structs. structures we already know that arrays are many variables of the same type grouped together...

Download 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

Post on 27-Dec-2015

212 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Structs

  • StructuresWe 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.

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

  • Creating a variable of struct typeThe 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:

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

  • Accessing the elements of a structureTo 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); }

  • Struct formatThe 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.

  • 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

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

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

  • Another exampleHere is an example program:

  • 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; }

  • Employee Record

    ID_numberAgeSalary12212000.10

  • Type definitionsYou 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 structureIf 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; }

  • ExerciseCreate a person struct with fieldsName, Address, Student no. Grade.

    Create 2 variables of type personAssign values to each of their fields.Print out who has the highest grade.

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

  • main(){person p1,p2;p1.name = fredp1.address = kevin stp1.student_no = c102445p1.grade = 72;

  • p2.name = maryp2.address = bolton stp2.student_no = b10775p2.grade = 87;if (p1.grade > p2.grade){printf(freds mark %d is greater than marys %d, p1.grade,p2.grade);}else{printf(marys mark %d is greater than freds %d, p2.grade,p1.grade);}}

  • ExerciseCreate a struct which represents a book.

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

  • Can have an array of these structse.g. catalog is an array of 100 books

    struct book catalog[100];

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

  • #include #include

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

  • 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);}}

  • NoteWe can also define pointers to structsstruct book *book1;

Recommended

View more >