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

sizeof() operator in C++

Why do we write sizeof(x) / sizeof(x[0]) instead of simply writing sizeof(x) to determine the size of the array in C++

I was expecting it to give correct output if we will write only sizeof(x) to find the size of the array.

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 :

Because sizeof uses units of bytes, not objects. These days, for array’s it’s better to just use std::extent to get array dimensions (the linked page includes examples). Separately, there was no need for parentheses when the argument is an object, so sizeof x / sizeof x[0] suffices. Parentheses are needed for types.

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