Structure Basics in Programming :: C++ Programming Structure Basics
79Requirements
- Computer
- Compiler
- Programmer
- Basic knowledge of programming
Collection of heterogeneous data types are known as Structure.
Structure is one of the data types of C/C++ programming languages. Several data types are grouped together in structure.The basics of structure and the initialization process will be discussed in this part.
What is Structure / Structure Programming in C/C++ language?
In C/C++ programming structure can be used. Its main goal is to group several heterogeneous data types. It can be stated as:
- collection of heterogeneous data types which are grouped together
- referred as member: single unit of structure
- each member specified by: variable name + period (.) + member name
- definition specified by → "struct" keyword every structure
- block definition is finished by a semicolon (;)
Example of structure (Very Simple):
We want to build a structure based on company's employee information. Now for this the definition of structure will be:
/* definition of structure & members declaration */
struct company{
long int employee_id;
int age; char sex;
float salary;
};
/* definition of object of the following structure */
struct company employee;
General format of a structure
storage_class struct user_defined_name{
data_type member 1;
data_type member 2;
........
data_type member n;
};
Graphical representation
Symbolic representation
Representation of a structure:
(a) Graphical representation
(b) Symbolic representation
Initialization of structure
Can be done by 2 ways:
- Static initialization
- External initialization
(a) Static initialization
All the member values are assigned here.
|
Introduction to Engineering Programming: In C, Matlab and Java
Price: $95.67
|
|
c-jump Computer Programming Board Game
Price: $29.99
|
|
Bluetooth Application Programming with the Java APIs Essentials Edition (The Morgan Kaufmann Series in Networking)
Price: $25.00
List Price: $39.95 |
|
Mastering XMI: Java Programming with XMI, XML, and UML (With CD-ROM)
Price: $19.00
List Price: $95.00 |
Static initialization :: Example
A structure contains the information of employee_id, age, sex and salary. So, the structure looks like the followings:
struct company{
long int employee_id;
int age;
char sex;
float salary;
}employee;
Here "employee" is the variable tag. Initialization can be done one of the following ways:
employee.employee_id = 1000;
employee.age = 30;
employee.sex = 'M';
employee.salary = 30000;
OR
struct company employee2 = {1000, 32, 'F', 20000};Output of the program
Note:
If any member is not initialized then that member is assigned with 0 values. For example, if the initialization is:
struct company employee = {1000};Then, the members are initialized as follows:
employee.employee_id = 1000;
employee.age = 0;
mployee.sex = 0;
employee.salary = 0;
Output of the above code segment
(b) External initialization
For this case, the entire member's values are assigned by the user. For the above structure, the values are collected from the user by the following way:
cout << "ID :"; cin >> employee.employee_id; cout << "Age :"; cin >> employee.age; cout << "Sex :"; cin >> employee.sex; cout << "Salary :"; cin >> employee.salary;
Output of the above code segment
More tutorials on "Structure"
- Nested Structures : Structures within Structures
Programming Tutorials : C/C++ Programming Structures can be implemented with functions and arrays. Moreover, structures can be implemented as the member of other structure. This is termed as structure within... - Write a C/C++ program for displaying today's date by...
Programming Tutorials : C/C++ Programming It's a simple program to start learning structure in C/C++. This program will display today's date. Suppose today's date is 30.10.2008. So, the output of the program... - Structues and Functions
Programming Tutorials : C/C++ Programming Function decomposes any complex program into several manageable modules. Each module is referred as function. Functions are compiled separately. So, they can be... - Structures and arrays
Programming Tutorials : C/C++ Programming Structure and array can be implemented together. An array is a group of identical data items. All array elements are sorted consecutive memory location under a...
Structue in C/C++ programming
Is this tutorial is helpful for building your basics?
See results without votingMr recent activities
- Fresh and unique contents for Search Engine Optimization: The secret talks of SEO and generating site contents
Millions of websites with billions of webpages are available in the cyberspace. But only a few of the pages are getting real popularity everyday. Google, Yahoo and Bing are the major search engine.... - 2 weeks ago
- Free malware protection with Portable Anti Trojan Elite: The best malware blocker and malware remover for your compter
Recently malware is spreading very rapidly. Malware is a type of software which is designed specifically to damage or disrupt a system, such as a trojan horse or a spyware or a keylogger. If your PC... - 2 weeks ago
- rundll32.exe - Application Error â The memory could not be "read". Click on OK to terminate the program.
Windows operating system is corrupted due to several virus infections. One such type of common infection is rundll32.exe - Application Error. Due to virus infection, rundll32.exe or such type of... - 2 weeks ago
PrintShare it! — Rate it: up down flag this hub









