• Advertisement

Archived

This topic is now archived and is closed to further replies.

Arrays && confused.

This topic is 5311 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Someone was looking over some code that I wrote and brought something to my attention. I have a code something like this: This is oversimplified. int array[3]; for(int i = 0; i < 3; i++) { Do stuff with array; } Now, he keeps tells me that I''m running over the bounds of my array, because I am declaring 3 int variables, indexes 0 to 2. I always thought that it was 0 to 3, and when I''ve experimented with it, my programs won''t crash unless I were to raise i to 4 and try to do stuff with the data there. Am I right? Or have I been misguided and just been extremely lucky to not have had a problem with this coding style for many years?

Share this post


Link to post
Share on other sites
Advertisement
Using array[3] would be overrunning the bounds, but your example stops iterating after using array[2].

Share this post


Link to post
Share on other sites
I think if you had

for(int i = 0; i <= 3; i++)
{
Do stuff with array;
}

would be going out of array bounds.

Your stopping at 2 so it''s fine.

Share this post


Link to post
Share on other sites
quote:
Original post by Daishi
int array[3];

for(int i = 0; i < 3; i++)
{
Do stuff with array[i];
}
This is correct; i will take the values 0, 1, 2, which is within the array's bound. If you count 0, 1, 2, it is three elements, exactly like you declared it.
quote:
Now, he keeps tells me that I'm running over the bounds of my array, because I am declaring 3 int variables, indexes 0 to 2. I always thought that it was 0 to 3
You thought you could address elements 0, 1, 2 and 3 (that is, four elements), even though you only declared your array to contain three elements? Pretty self-evident that you shouldn't.
quote:
and when I've experimented with it, my programs won't crash unless I were to raise i to 4 and try to do stuff with the data there.
You were just lucky.

It is very easy. When you declare int array[n ];, your array will have n elements. And since it is zero-based, it will range from 0 to n -1.

[edited by - CWizard on October 5, 2003 7:13:48 PM]

Share this post


Link to post
Share on other sites
Ok, I wasn''t exactly too clear, and I had a few typos. The friend that was looking over my code kept insisting something along the lines of... you declare array[3]. That gets you 0 to 2. So you have 2 indexes. (That''s where my typo was). He flipped through about 6 header files in his example using more malloc functions than I had ever seen in a seemingly useless program and was said ''this is this'' and ''that is this'' so this only yields you two indexes. He was confusing me to no end, and it appears he was wrong anyway. array[3] yields me 0 to 2, which is enough for the i < 3 (0-2, 3 indexes). His logic didn''t make sense and I had to prove it to him.

Share this post


Link to post
Share on other sites

  • Advertisement