0% found this document useful (0 votes)
2 views

Chapter 2 - StructuresConcise

The document provides an overview of structures in programming, defining a structure as a collection of data members with similar or dissimilar data types. It explains how to define, declare, and initialize structures, as well as how to access their member variables. Examples are provided to illustrate the creation and usage of structures, including student and date structures.

Uploaded by

kahsay
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter 2 - StructuresConcise

The document provides an overview of structures in programming, defining a structure as a collection of data members with similar or dissimilar data types. It explains how to define, declare, and initialize structures, as well as how to access their member variables. Examples are provided to illustrate the creation and usage of structures, including student and date structures.

Uploaded by

kahsay
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Assosa University Department Of Computer

Science
Chapter Two
Structures

1. What is a Structure?
A structure is a collection data structures that contains member elements or member variables known
as data members with similar or dissimilar data types that can be referred with single or group
name and member name.

2. struct Specification: Defining Structures


Defining a structure is giving the compiler a blue print for creating your type.
When you create a variable based on the structure definition, all of the member
variables are created automatically and grouped under the name you gave. Note
that when you create a variable of any kind, you must give it a unique name that
is different than its type.
A structure definition is a user-defined variable type which is a grouping of one
or more variables.
The structure definition is a listing of all member variables with their types and
names.
Below is the syntax for a structure definition:

struct structname
{
datatype1 Membervariable1;
datatype2 Membervariable2;
“ “
datatypeN MembervariableN;
};

The data members (synonym for member variables) of a structure won’t actually be
created until a variable based on the structure is created. Example defining a student
struct,

struct student
{
int id;
char name[15];
};
Example: The follow defines a structure called ‘date’ which contains three ‘int’ member
variables: ‘day’, ‘month’, and ‘year’:

struct date {
int day;
int month;
int year;
};
struct date
{
int day, month, year;
};
Note: You cannot initialize member variables in a structure definition.
The following is wrong and will not compile:
struct date{

-1-
Assosa University Department Of Computer
Science
int day = 24, month = 10, year = 2001;
};

#include <iostream.h>
struct date {
int day, month, year;
};
int main() {
date d; //creating structure object d to access the data members in the
structure date
return 0;
}

3. Declaring and using sturct data types


Once you have defined a structure you can create a variable from it just as you
would any other variable.

student std1;
date birthday;
int i;

The above statements are similar in nature because both declare a variable of a
given type. The former is a built in type, which is integer while the later
declares a variable type of user-defined type. std1 will have the characteristics
of representing id and name of a student.

4. Initializing Structure Variables


Values are assigned to member variables in the order that they occur. For
example:

date birthday = { 19, 8, 1979 };


student std1={"Abebe", "ETR/390/04",Male”,3.25};
student std2={"Fikir", "ETR/410/04",Female”,3.0};
student std3={"Rahel", "ETR/420/04",Female”,3.75};

5. Accessing members of a structure variable


You can use a member variable in any place you’d use a normal variable, but
you must specify it by the structure variable’s name as well as the member
variable’s name using the member operator.
structurename.datamember;

Example: For reading and displaying values to and from structure s1.

cin>>s1.id; //storing to id item of s1


cin>>s1.name; //storing a name to s1
cout<<s1.id; //displaying the content of id of s1.
cout<<s1.name; //displaying name

-2-
Assosa University Department Of Computer
Science

Example:-a program that creates student struct and uses it to The above program shows you how to capture data in student variables.
store student information. Example 2: This program demonstrates using member variables for user input,
output, and mathematical operations.

#include<iostream.h> #include <iostream.h>


#include<conio.h> struct date
struct student
{
{
int id; int day, month, year;
char name[15]; };
}; int main()
{
int main() date birth;
{ cout << “Enter your birth date: ” << endl;
//creating two student structure cout << “Year: “;
objects
cin >> birth.year;
student s1,s2;
cout<<"\n Enter Student Id"; cout << “Month: “;
cin>>s1.id; cin >> birth.month;
cout<<"\nEnter Name"; cout << “Day: “;
cin>>s1.name; cin >> birth.day;
cout<<"\n Enter Student Id"; cout << “You entered “ << birth.month << “/”<<
cin>>s2.id; birth.day
cout<<"\nEnter Name"; << “/” << birth.year << endl;
cin>>s2.name;
cout << “You were born in the “<< ((birth.year / 100)
cout<<"\nStudents Information"; + 1)
cout<<"\n Student id\t Student << “th Century!”<< endl;
Name"; return 0;
cout<<endl<<s1.id<<"\ }
t"<<s1.name;
cout<<endl<<s2.id<<"\
t"<<s2.name;
return 0;
#include<iostream.h>
} If you use s[0].id you will be referring to 1, and s[0].name will refer to
Tameru.
#include<conio.h> The following program declares and uses a book struct. Also swaps
struct student { the content of two book struct variables.
int id;
char name[15]; #include<iostream.h>
}; #include<conio.h>
struct Book {
int id;
int main()
char title[15];
{ };
//creating 5 student using an array int main(){
student s[5]; //creating three Book variables
Book b1,b2,temp;
int i; cout<<"\n Enter Book Id";
for(i=0; i < 5; i ++) cin>>b1.id;
{
6. Array of structs
cout<<"\n Enter Student Id";
cout<<"\nEnter Title";
cin>>b1.title;
cout<<"\n Enter Book Id";
cin>>s[i].id; cin>>b2.id;
cout<<"\nEnter Name"; cout<<"\nEnter Title";
cin>>s[i].name; cin>>b2.title;
} cout<<"\n Book Information";
cout<<"\n Before Changing Contents";
cout<<"\n Displaying student Info"; cout<<"\n Book id\t Title";
cout<<"\nStudent Id \t Student cout<<"\
Name"; n=========================";
cout<<"\ cout<<endl<<b1.id<<"\t\t\t"<<b1.title;
cout<<endl<<b2.id<<"\t\t\t"<<b2.title;
n============================"; //swapping content
temp=b1;
for( i = 0; i < 5; i++) b1=b2;
cout<<endl<<s[i].id<<"\t\t\t"<<s[i].name; b2=temp;
cout<<"\nAfter swapping contents";
return 0; cout<<"\n Book Information";
} cout<<"\n Book id\t Title";
cout<<"\
n=========================";

cout<<endl<<b1.id<<"\t"<<b1.title;
cout<<endl<<b2.id<<"\t"<<b2.title;
-3- return 0;
}
Assosa University Department Of Computer
Science

-4-

You might also like