Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Zero Initialization in C++
Zero initialization is setting the initial value of an object in c++ to zero.
Syntax
T{} ;
char array [n] = “”;
The situations in which zero initialization are performed are −
Named variable with static or thread-local storage is initialized to zero.
It is used as initialization of values for non-class types and members of a class that do not have a constructor.
It is used to initialize a character array when its length is greater than the number of characters that are to be assigned.
Points to remember
Some types of variables like static variables and thread-local variables are first initialized to zero then reinitialized to a value when called.
A zero-initialized pointer is called a null pointer.
Example
Program to show the implementation of zero initialization in C++ −
#include <iostream>
#include <string>
using namespace std;
struct zeroInitialization {
int x, y, z;
};
float f[3];
int* p;
string s;
int main(int argc, char* argv[]){
zeroInitialization obj = zeroInitialization();
cout<<"Zero initialized object variable :\t";
cout<<obj.x<<"\t"<<obj.y<<"\t"<<obj.z<<"\n";
cout<<"Zero initialized float value :\t";
cout<<f[0]<<"\t"<<f[1]<<"\t"<<f[2]<<"\n";
cout<<"Zero initialized pointer value :\t";
cout<<p<<"\n";
return 0;
}
Output
Zero initialized object variable : 0 0 0 Zero initialized float value : 0 0 0 Zero initialized pointer value : 0
Advertisements