Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I set 'null' value explicitly in integer array element in C++

Like in Java, there’s a way to set null values in array elements,

Integer[] arr = new Integer[5];

In here, arr is an Integer array with size 5 contains null by default

So can we do the same in C++?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In Java, Integer is an object type, not a primitive type, so it’s allowable to to set one to null. C++ doesn’t have equivalent object types. A better analogue would be the following java:

int[] arr = new int[5];

Here you have a primitive int type which can’t be set to a null value. In C++ it’s the same thing.

The closest you can do is set all values to 0 or some other sentinel value. Either that or create your own Integer type which I wouldn’t advise.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading