25
Structs

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

Embed Size (px)

Citation preview

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 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 struct

• Here is how you would create that person structure in C:

• struct person{   char name[30];

   int age;};

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:

Program

• #include<stdio.h> struct person{  char name[30];   int age;}; main(){   struct person p;}

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<stdio.h> 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 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.

• 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 example

• Here 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_number Age Salary

1 22 12000.10

• 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<stdio.h>

 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<stdio.h>

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

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.

#include <stdio.h>

struct person

{char name[40];

char address[80];

char student_no[10];

int grade;

}

main()

{

person p1,p2;

p1.name = “fred”

p1.address = “kevin st”

p1.student_no = “c102445”

p1.grade = 72;

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

Exercise

• Create a struct which represents a book.

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

Can have an array of these structs

e.g. catalog is an array of 100 books

struct book catalog[100];

Exercise 2

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

#include <stdio.h>

#include <string.h>

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

}

}

Note

• We can also define pointers to structs

• struct book *book1;