Structure Basics in Programming :: C++ Programming Structure Basics
:: Big Index :: C/C++ Programming Tutorials ::
Requirements
- 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.
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...