8/13/2019 ITEI222 - Better Structure Supplementals
1/30
1
Structures
Objectives: In this chapter you will learn about,Introduction
Declaring Structure Type & StructureVariables
Referring and initializing structure elements
Passing structures to a function
Using typedef
Example using structureEnumeration constants
8/13/2019 ITEI222 - Better Structure Supplementals
2/30
2
Introduction
So far we have only used data types which have
been defined by C such as int, double and char.It is also possible to create our own data types.
A user defined data type is called a structure.
A structure can contain both built-in data typesand another structure.
The concept of structure is pretty much thesame as arrays except that in an array, all the
data is of the same types but in a structure, thedata can be of different types.
8/13/2019 ITEI222 - Better Structure Supplementals
3/30
3
Definition
A structure is a derived data type that
represents a collection of a related dataitems called components (or members)that are not necessarily of the same data
type.
8/13/2019 ITEI222 - Better Structure Supplementals
4/30
4
General syntax:
struct structure_name {data_type element1;
data_type element2;
. . .
};Example:
struct student {
char name[20];
int studentID;char major[50];
};
Declaring Structure TypeAlso called as structure tag
Components / members
8/13/2019 ITEI222 - Better Structure Supplementals
5/30
5
Declaring Structure VariablesAfter declaring a structure type, we may
declare variables that are of that type. Astructure variable declaration requires:
The keyword struct
The structure type name
A list of members (variable names) separated bycommas
A concluding semicolon
Then, assume that variable of structure typestudent is my_student. So the declarationshould be written as;
struct student my_student;
8/13/2019 ITEI222 - Better Structure Supplementals
6/30
6
Based on example: struct student
By including this declaration in our program,
we are informing the compiler about a newdata type which is a user defined data type.
The declaration just makes the compileraware the existent of new data type but does
not take an action yet.
Based on the declaration of
struct student my_student;
causes the compiler to reserve memoryspace for variable my_student and thecomponents of its structure.
8/13/2019 ITEI222 - Better Structure Supplementals
7/307
Structure variable Components Values
name
major
studentID
Simon
0078
CS
my_student
Conceptual memory structure variable my_student of type
student(assuming that the components of variable
my_student have already been assigned values)
8/13/2019 ITEI222 - Better Structure Supplementals
8/308
It is possible to combine the
declarations of a structure type and astructure variable by including the nameof the variable at the end of the
structure type declaration.struct student {
char name[20];
int studentID;
char major[50];};
struct student my_student;
struct student {
char name[20];
int studentID;
char major[50];} my_student;
=
8/13/2019 ITEI222 - Better Structure Supplementals
9/309
Declaring Nested StructureMembers of a structure declaration can
be of any type, including anotherstructure variable.
Suppose we have the followingstructure declaration, which is a
member of struct type student:struct address {
int no;
char street[20];
int zipcode;
};
8/13/2019 ITEI222 - Better Structure Supplementals
10/3010
We can rewrite the structure student
declaration as follow:
This structure type student can be
written as;
struct student {
char name[20];
int studentID;
char major[50];struct address addr;
} ;
8/13/2019 ITEI222 - Better Structure Supplementals
11/3011
struct student {
char name[20];
int studentID;
char major[50];
struct address{
int no;
char street[20];int zipcode;
};
} ;
8/13/2019 ITEI222 - Better Structure Supplementals
12/3012
Referring and Initializing Structure Elements
A structure contains many elements. Each elements of astructure can be referred to / accessed by using thecomponent selection operator. (dot).
Let us use the structure student which we have seen before asan example:
Therefore to refer to the element of a structure, we may writeas follow;
my_student.name;my_student.studentID;my_student.major;
struct student {
char name[20];
int studentID;
char major[50];
};
struct student my_student;
8/13/2019 ITEI222 - Better Structure Supplementals
13/3013
Therefore, we can initialize each elements of
a structure individually such as:struct student my_student;my_student.studentID = 10179;
Or we can initialize the structure while we are
creating an instance of the structure:struct student my_student = {Jared, 10179, IT}
Notice that it is possible to use the =operator on a struct variable. When the =sign is used, each elements of the structureat the right hand side is copied into thestructure at the left hand side.
8/13/2019 ITEI222 - Better Structure Supplementals
14/3014
Examplestruct birthdate {
int month;
int day;
int year;
};
struct birthdate Picasso = {10, 25, 1881};cout
8/13/2019 ITEI222 - Better Structure Supplementals
15/3015
Passing Structures to a Function
Call by Value:
We can pass the student structure that we havecreated before to a function called display( ) asfollows:
void display (struct student); /* function prototype */
display (student1); /* function call */
void display (struct student s1); /* function header */where student1 is a variable of type struct student.
In the above function, a copy of the student structure
will be created locally for the use of the function.Anychanges to the structure inside the function will notaffect the actual structure.
8/13/2019 ITEI222 - Better Structure Supplementals
16/3016
Example Using Structure: Call by value
struct student{char name[20];
int id;};
void display(struct student); /* function prototype */
void main(){
struct student student1;
strcpy(student1.name, Jared"); /*initializing variable */student1.id = 12345; /*initializing variable */
display(student1);}
void display(struct student s1) /* make a local copy of the structure */{
cout
8/13/2019 ITEI222 - Better Structure Supplementals
17/3017
Example Using Structure: A Function that return a structure
struct student{char name[20];int id;
};struct student read(void); /* function prototype */
void main( ){
struct student student1;
student1 = read(); /*function call */
cout s1.id;
return s1;}
8/13/2019 ITEI222 - Better Structure Supplementals
18/30
8/13/2019 ITEI222 - Better Structure Supplementals
19/30
19
Take note that when a structure is declared as a
pointer, the elements in the structure cannot bereferred to using the . operator anymore. Instead,they need to be accessed using the -> operator(indirect component selection operator).
For example:void Read(struct student *s1)
{s1->studentID = 10179;cin >> s1->name;
}
8/13/2019 ITEI222 - Better Structure Supplementals
20/30
20
Example Using Structure: Call by reference
struct student{char name[20];int id;
};void Read (struct student *); /* function prototype*/
void main(void){
struct student student1;
Read(&student1); /* function call: passing reference */
cout > s1->name;cout \nEnter ID:;cin >> s1->id;
}
8/13/2019 ITEI222 - Better Structure Supplementals
21/30
21
Using typedefin Structure Declarations
The keyword typedefprovides a
mechanism for creating synonyms(aliases) for previously defined datatypes.
Here is an example on how to usetypedef when declaring a structure:
struct student {
char name[20];
int studentID;
char major[50];
struct address addr;
} ;
8/13/2019 ITEI222 - Better Structure Supplementals
22/30
22
By using typedef:
typedef struct student StudentData;
we are now aliasing the structure with aname to be used throughout the program. Soinstead of writing the word struct beforedeclaring a struct variable like the following
struct studentmy_student;
we can now write:
StudentDatamy_student;
We could use the alias name when passingthe structure to a function:
void display(StudentData s1);
8/13/2019 ITEI222 - Better Structure Supplementals
23/30
23
struct student{char name[20];int id;
};
typedef struct student StudentData;
void display(StudentData); /* function prototype */
void main(){
StudentDatastudent1;
strcpy(student1.name, Jared");student1.id = 12345;
display(student1);}
void display(StudentDatas1){
cout
8/13/2019 ITEI222 - Better Structure Supplementals
24/30
24
Example: Array of structure#define NUM_STUDENTS 10struct student {
int studentID;
char name[20];int score;char grade;
};typedef struct student StudentData;
void Read (StudentData student[]);void CountGrade (StudentData student[]);
void main ( ){
StudentData student[NUM_STUDENTS];
Read(student);CountGrade(student);
}
8/13/2019 ITEI222 - Better Structure Supplementals
25/30
25
void Read (StudentData student[]){
int i;for (i = 0; i < NUM_STUDENTS; i++) {
cout > student[i].name;cout
8/13/2019 ITEI222 - Better Structure Supplementals
26/30
26
void CountGrade (StudentData student[])
{
int i;
for (i = 0; i < NUM_STUDENTS; i++) {
if (student[i].score > 90)
student[i].grade = 'A';
else if (student[i].score > 80)
student[i].grade = 'B';
else if (student[i].score > 65)student[i].grade = 'C';
else if (student[i].score > 50)
student[i].grade = 'D';
else
student[i].grade = 'F';cout
8/13/2019 ITEI222 - Better Structure Supplementals
27/30
27
/* Sample Output
Enter the studentID: 789654
Enter the name: Salman
Enter the score: 96
Enter the studentID: 741258
Enter the name: Jack
Enter the score: 79
::
:
The grade for Salman is A
The grade for Jack is C:
:
Press any key to continue
*/
E ample
8/13/2019 ITEI222 - Better Structure Supplementals
28/30
28
Examplestruct car{
char maker[20];
char model[20];int year;
};
void input(struct car*);
void output(char*, char*, int*);void main()
{
struct car firstcar;
input (&firstcar);
output(firstcar.maker, firstcar.model, &firstcar.year);printf("End of my act!\n");
}
8/13/2019 ITEI222 - Better Structure Supplementals
29/30
29
void input(struct car *sp)
{
printf("What is the maker of your car? ");gets(sp->maker);
printf("What is the model of your car? ");
gets(sp->model);
printf("What year is your car? ");
scanf("%d", &sp->year);
}
void output(char *sp1, char*sp2, int*sp3)
{
printf("Your car is : %s, %s, %d\n", sp1, sp2, *sp3);
printf("Nice car\n");}
8/13/2019 ITEI222 - Better Structure Supplementals
30/30
/* Sample output
What is the maker of your car? HondaWhat is the model of your car? Stream
What year is your car? 2003
Your car is : Honda, Stream, 2003
Nice carEnd of my act!
Press any key to continue
*/
Top Related