Structure Basics in Programming :: C++ Programming Structure Basics

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

Graphical representation of structure
Graphical representation of structure

Symbolic representation

Symbolic representation of structure
Symbolic representation of structure

Representation of a structure:

(a) Graphical representation

(b) Symbolic representation  

Initialization of structure

Can be done by 2 ways:

  1. Static initialization
  2. 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

Output of the program
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

Output of the above program
Output of the above program

(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

Output of external initializations above program
Output of external initializations above program

Structue in C/C++ programming

Is this tutorial is helpful for building your basics?

  • Yes
  • No
  • Nothing to say
See results without voting

More by this Author


Comments 6 comments

kibreab 6 years ago

It is a very nice tutorial that build my basics.

thanks in advance


Urmale 5 years ago

I found it an interesting tutorial.

thanks.


Irfan Ahmad 5 years ago

Its resly nice,it solve my problems.

Thanks.


mohammad kamran  5 years ago

yes, it is very helpful in learning structures


shubhkaran 4 years ago

Selection sort Merge Sort insertion delation addition in programming in c++ data structure


NH 4 years ago

nice1

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working