Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Why we use zero always at the start of any array in C++?

0 votes
asked Aug 23, 2020 by Muhammad Abbas (120 points)

2 Answers

+2 votes
answered Aug 25, 2020 by LiOS (6,420 points)

We start at an index of 0, in a lot, if not all, of today's coding languages.

The use of index is to describe the offset from the beginning. While we describe the 1st value in an array as element 1, we obtain it using the index of 0 since it's right at the beginning.

This link may give you a better understanding: https://developerinsider.co/why-does-the-indexing-of-array-start-with-zero-in-c/

+1 vote
answered Aug 26, 2020 by Peter Minarik (86,200 points)

Just in case if you meant zeroing an array in C/C++

char myBuffer[20] = { 0 };

This is to set the memory occupied by myBuffer all to 0 (or '\0', or terminating zero) to remove any previous content from that memory address and make sure we know what's there. A terminating zero indicates the end of a string, so the above code effectively sets myBuffer to be empty.

If you meant why do we start indexing an array at 0 index (instead of 1), that is, why the first element is at position 0, not 1, please see LiOS's answer.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...