Structure Basics in Programming :: C++ Programming Structure Basics
86Requirements
- 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: $68.30
|
|
c-jump Computer Programming Board Game
Price: $23.75
List Price: $29.99 |
|
Java
Price: $68.08
List Price: $72.08 |
|
Programming with Java w/ CD-ROM
Price: $8.44
|
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
- PPV or PPC?: "Pay Per View or Pay Per Click?" - Which One is Better?
Pay per view (PPV) and pay per click (PPC) are two types of marketing advertising solutions which is used by the advertisers to reach targeted group of visitors. A basic introduction will be... - 4 hours ago
- Make Good Money with Good Articles and Get Quick Cash
Before going to the main content, I want to ask you tow question? (a) How to make money on internet? (b) How to make money quick? I think, you have some idea. One thing, if you don't concern about... - 22 hours ago
- Hot Secrets Behind Good Articles: How and Why Good Writers Always Write Good Articles?
Before writing to Hubpages, I have no idea about good articles. However, I have achieved it. Thanks hubpages. I already published several articles and I get about 2,000 visitors for my contents each... - 2 days ago
PrintShare it! — Rate it: up down flag this hub









