C-and-C++/C4/Working-With-Structures/English
Title of script: Structures
Author: Ashwini Patil
Keywords: Strucutre, structure variable, Video tutorial.
|
|
Slide 1 | Welcome to the spoken-tutorial on Structures in C and C++. |
Slide 2
Learning Objectives |
In this tutorial we will learn what,
Is a Structure. Declaration of a structure We will do this through an example
|
Slide 3
System requirements |
To record this tutorial, I am using,
Ubuntu Operating System version 11.04, gcc and g++ Compiler version 4.6.1 |
Slide 4
Introduction |
Let us start with the introduction to Structure
When one or more variables are grouped under one name it is known as structure. Strucutre is used to group different data into one object. It is called as compound data-type. It is used to group related information together. |
Slide 5 | Now we will see
The syntax to declare a structure Here the keyword struct tells the compiler that a structure is declared strcut_name is the name of the structure. eg. struct employee; You can give any name. |
Slide 6 | Now we will see how to declare a structure variable.
Syntax for this is struct struct_name struct_var; struct_var is the variable of type struc_name eg. struct employee addr; addr is the variable of type employee. |
Let us move on to our example | |
I have already typed the program on the editor
So let me open it. | |
Point to struct.c | Note that our filename is structure.c |
In this program we will calculate the total marks of three subjects using structure | |
Let me explain the code now. | |
Highlight
#include <stdio.h> |
This is our header file. |
Highlight
struct student |
Here we have declared a structure as student.
|
Highlight
int eng; int maths; int science; |
Then we have declared three integer variables as english, maths and science.
Variables defined under the structure are called as members of the structure. |
Highlight
int main() |
This is our main function |
Highlight
int total |
Here we have declared an integer variable total. |
Highlight
struct student st; |
Now we have declared a structure variable stud, stud is the variable of type student,it is used to access and modify the structure members
|
Highlight
st.eng = 75; st.maths = 70; st.science = 65; |
Here we have modified the members
By assigning them values of 75, 70 and 65. |
Highlight
total = st.eng + st.maths + st.science; |
Here we calculate the total of the three subjects. |
Highlight
printf("Total is %d\n",total); |
Then we print the result. |
Highlight
return 0; |
This is our return statement. |
Click on Save | Now click on Save |
Let us execute the program | |
Press Ctrl, Alt and T keys | Please open the terminal window by pressing
Ctrl, Alt and T keys simultaneously on your keyboard. |
Type
gcc struct.c -o struc
./struc |
To compile
Type gcc space structure.c space -o space struct And press Enter To execute Type ./struct Press Enter |
Highlight
Output |
The output is displayed as:
Total is 210 |
In C++ | Now we will execute the same program in C++ |
On the editor | Come back to our program. |
I will edit the same code | |
Click on Save as option
Type .cpp |
First click shift, cntrl adn S keys simultaneously on the keyboard
Now save the file with an extension .cpp and click on save |
Type
iostream |
Lets change the header file as iostream |
Type
using namespace std; |
Now include the using statement
and click on save |
Structure declaration in C++ is same as in C
So no need to change anything here | |
Type
cout << |
At the end we will replace the printf statement with the cout << statement. |
Delete \n and ,
<< << “\n” |
Delete the format specifier and \n
Now delete the comma Type two opening angle brackets. Here delete the closing bracket and type two opening angle backets And within the double quotes type \n |
Click on Save | Now click on Save |
Let us execute the program | |
On the terminal
Type g++ struc.cpp -o st2 Type ./st2 |
Come back to our terminal
To compile type g++ space structure.cpp space -o space struct1 Here we have struct1 because we dont want to overwrite the output parameters for the file structure.c Now press Enter To execute type ./struct1 Press Enter |
Highlight
Output |
The output is displayed as:
Total is 210 You can see that the output is same as our C code Now come back to our slides. |
Slide 7
Summary |
We will summarize now. In this tutorial we learned,
Structure. Syntax of a structure. eg. struct struct_name; To access members of a structure. Eg: stud.maths = 75; To add the structure variables. Eg: total = stud.eng + stud.maths + stud.science; |
Slide 8
Assignment |
As an assignment,
Write a program to diplay the records of an employee. Like name, address, designation, salary. |
Slide 9
http://spoken-tutorial.org /What\_is\_a\_Spoken\_Tutorial About the Spoken Tutorial Project |
Watch the video available at the link shown below
It summarises the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it |
Slide 10
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to contact@spoken-tutorial.org |
Slide 11
|
Spoken Tutorial Project is a part of the Talk to a Teacher project
It is supported by the National Mission on Education through ICT, MHRD, Government of India More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
Ashwini Patil from IIT Bombay
Thank You for joining |