You are on page 1of 21

CHAPTER 4

ARRAY & STRUCTURES


4.2
STRUCTURES
COURSE LEARNING OUTCOMES :

CLO1:
Apply program structure and debugging process in C++
programming language accordingly.
CLO2:
Design programs using appropriate control structures,
arrays, structures, functions and pointers.
CLO3:
Solve problems using C++ programming language
environment with proper coding style guidelines and take in
the security issues into consideration.
What is a Structure ?
• A structure is a group of data elements grouped
together under one name.

• These data elements, known as members, can


have different types and different lengths.
Difference between
Structure & Array
• Arrays are collections of the same data
type
• Structure is a collection of variables under
a single name
How to Declare Structures
• Syntax (uses keyword ‘struct’)

struct structure_name
{
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
How to Declare Structures
• Explanation for the syntax
- structure_name is a name for the structure type, object_name
can be a set of valid identifiers for objects(variables) that have
the type of this structure.
- Within braces { } there is a list with the data members, each
one is specified with a type and a valid identifier as its name.
- Once a data structure is declared, a new type with the
identifier specified as structure_name is
created and can be used in the rest of
the program as if it was any other type.
How to Declare Structures
Sample :
keyword

structure name

struct Employer
{
int emp_id;
char emp_name[30]; structure members

float salary;
};
How to Declare Structures
• In the previous sample, it is seen that variables of
different types such as int, float and char are
grouped in a single structure name Employer
• After declaring the structure, the next step is to define
a structure variable
How to Declare Structures
• This is similar to variable declaration.
• For variable declaration, data type is defined
followed by variable name:
int name;
• For structure variable declaration, the data type is
the name of the structure followed by the structure
variable name .
How to Declare Structures
• From the previous sample

Structure name

Employer one;
Structure
variable name
How to Declare Structures
What happens when this is defined?

• It allocates or reserves space in memory


• The memory space allocated will be cumulative of
all defined structure members
More appropriate codes

struct Employer {
int emp_id;
char emp_name[30];
float salary;
};
Employer one;
How to access structure
members?
• To access structure members, the operator used is
the dot operator denoted by (.)
• If we want to assign 2000 for the structure member
salary :
one.salary = 2000;

Structure variable structure members


name
Complete C++ Program on How to
access structures members
#include <iostream.h>
void main()
{
struct student
{
char name[30];
char regnum[12];
int age;
};
student id;
cout << "Enter student name : ";
cin >> id.name;
cout << "Enter reg num : ";
cin >> id.regnum;
cout << "Enter age : ";
cin >> id.age;
}
How to retrieve data from the
structure members?
• The same way we accessing them
• Eg:
id.name;
Complete C++ Program
(Cont.. from previous program)
#include <iostream.h>
void main() cout <<"\nYou have entered :";
{ cout <<"\nName :"<<id.name;
struct student cout <<"\nReg Num :"<<id.regnum;
{ cout <<"\nAge :"<<id.age;
char name[30]; }
char regnum[12];
int age;
};
student id;
cout << "Enter student name : ";
cin >> id.name;
cout << "Enter reg num : ";
cin >> id.regnum;
cout << "Enter age : ";
cin >> id.age;
Complete C++ Program (multiple data)
Sample 1 :
#include <iostream.h>
void main()
{ for ( i = 0; i < 2; i++ )
struct student {
{ cout <<"\nYou have entered :";
char name[30]; cout <<"\nName :"<<id[i].name;
char regnum[12]; cout <<"\nReg Num:"<<id[i].regnum;
int age; cout <<"\nAge :"<<id[i].age;
}; }
student id[2]; }
for (int i = 0; i < 2; i++ )
{
cout << "Enter student name : ";
cin >> id[i].name;
cout << "Enter reg num : ";
cin >> id[i].regnum;
cout << "Enter age : ";
cin >> id[i].age;
}
Sample 2 :
#include <iostream.h>
#include <cstring.h>
struct person
{
int id;
string name;
};
int main()
{
person testst[2];
testst[0].id=1;
testst[0].name="Ali";
testst[1].id=2;
testst[1].name="Ahmad";
cout<<"For id ="<<testst[0].id <<",the person name is"<<testst[0].name<<endl;
cout<<"For id ="<<testst[1].id <<",the person name is"<<testst[1].name;
}
Try this!!!
• Define a type named Course that would
appropriate for storing course information.
Include components named code (for
example, F2037), course name(array of
character), credit hour( an integer)
Answer :
#include<iostream.h>

void main()
{
struct Course
{
char code[6];
char course_name[30];
int credit_hour;
}subject;

cout<<"Enter code:";
cin>>subject.code;

cout<<"Enter course name:";


cin>>subject.course_name;

cout<<"Enter credit hour:";


cin>>subject.credit_hour;

cout<<"Code:"<<subject.code<<endl;
cout<<"Course name:"<<subject.course_name<<endl;
cout<<"Credit hour:"<<subject.credit_hour<<endl;
}

You might also like