Effect of partial or no intialisation of structure instance variables – Computer Sir Ki Class


Lost your password?

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


Code Learning #CPP#2799 siteicon   siteicon   siteicon  

Effect of partial or no intialisation of structure instance variables

Understanding default values in case of partial or no initialisation of structure instances.

Learning Objectives

  • Partial initialisation of variables of a structure when its instance/object is created.
  • No initialisation leads to junk values.

Source Code

TC++ #2799


Source Code

Run Output

1  0 0
36 ╕■( 1967191250 1.12104e-44 //This line is junk output could be different in your case

Code Understanding

struct Book { int ID; char name[50]; int year; float price;};
Structure named Book declared with corresponding variables.

//Book b1={1,”Freedom at midnight”,1975,200.25};
This full initialisation example already discussed code sample at #2795  This is commented here in this example.

Book b1={1};
Partial initialisation example with only first value initialised, other are left for default initialisation. Default initialisation is all primitive data type of numbers will be made 0 and char and char array will become null.

Book b1={1,”Freedom at midnight”};
Book b1={1,”Freedom at midnight”,1975};
These are other partial initialisation example. Remember that only trailing variables can be left un-initialised means if third variable is initialised you should not leave the first and second one.

cout<<b1.ID<<” “<<b1.name <<” “<<b1.year<<” “<<b1.price<<endl;
You will see that ID as initialised will be printed while name will not print as it is initialised with null while year and price will be printed as 0.

Book b2;
This is case where another instance has been created but not initialised.

cout<<b2.ID<<” “<<b2.name <<” “<<b2.year<<” “<<b2.price<<endl;
Here junk values will be printed.



  • There is a syntax for all default initialisation. This is –
    Book b1={ };
    Putting curly braces does the whole trick.


Common Errors

  • Sometimes students try to intialise the intermediate initialisation assuming that it will work like this –
    Book b1={,”Freedom at midnight”,,200.25};
    This won’t work.

Suggested Filename(s): str-part-init.cpp,stprtini.cpp


sunmitra| Created: 19-Jan-2018 | Updated: 19-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