Structures demo program with int and char array – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#2767 siteicon   siteicon   siteicon  

Structures demo program with int and char array

A program using structure with two different type of variables int and char[] grouped in one structure.

Learning Objectives

  • Understanding the basic concept of a structure type by using a simple example.

Source Code

TC++ #2767


Source Code

Run Output

Give Name 1 : Laila
Give Name 2 : Majnu
Laila 21
Majnu 23

Code Understanding

struct Person //structure definition start with the struct keyword followed by name of structure
{                           //Strructure is a data-structure where members are enclosed in curly braces.
int age;               //This is to store the integer value of age
char name[50]; //This is to store char array which will contain the name.
};                          //The semicolon is always suffixed to an structure definition.

Person P1,P2;  
This instruction declares two data structure objects/instances that will have the structure like the person which means we can store two age and two name, one in P1 and another in P2 which are independent of each other.

Here we are initialising the age variable of two structure instances. We follow the . notation to connect the structure instance name to its variable name like P1.age or P2.age.

cout<<“Give Name 1 : “; cin.getline(,50);
Here we are collecting the name for first instance P1.

cout<<“Give Name 2 : “;  cin.getline(,50);
Here we are collecting the name for second instance P2.

cout<<<<” “<<P1.age<<endl; cout<<<<” “<<P2.age<<endl;
Name and age is being printed by using the instance.variable format.


  • None of the variable in the structure can be initialised while defining a struct{ }; syntax. This is because structure doesn’t take memory space. Memory space is actually reserved when structure instance is created like Person P1, P2;
  • The char array defined in structure can not be initialised after instance creation. This is because the structure arrays simply pointing to memory locations. So we can not write things like –”Laila”; The way can do otherwise in normal programming.

Suggested Filename(s): strudemo.cpp


sunmitra| Created: 18-Jan-2018 | Updated: 18-Jan-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues