Public Group

# Arrays

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

## Recommended Posts

billy[0] = a; billy[a] = 75; Here is my question about my new-found knowledge of arrays. The top one I understand as it sets the first BILLY array to the value of A. The second one I do not understand. Assuming you set it to int billy[5]; , how can there be an A slot. Aren't there only 0,1,2,3 and 4 slots?

##### Share on other sites
This code is a bit strange. When you define an array:

e.g billy[5], you get billy[0,1,2,3,4,5]

But I don't understand this code... I can't even see it working. I think the point that the code is trying to make is:

int billy[5];
int a = 3;
billy[0] = a;
billy[a] = 265;

That code would set the first part of the array to 3 because the variable a is put into it and the billy[3] to 265 because the variable a is used to pick a section, in this case the third.

Hopefully that makes sense to you.

##### Share on other sites
If you defined the array as int billy[5], then yes, you only have 5 slots. That's the the 5 means. Accessing anything outside this range is undefined. It can crash, it can work as expected; you never know what will happen. So don't do it.

##### Share on other sites
Quote:
 Original post by Gallivanbilly[0] = a;billy[a] = 75;Here is my question about my new-found knowledge of arrays.The top one I understand as it sets the first BILLY array to the value of A.The second one I do not understand. Assuming you set it to int billy[5]; , how can there be an A slot. Aren't there only 0,1,2,3 and 4 slots?

the character 'a' is converted to its decimal equivalent as laid out in the ASCII table, as such you'll end up with access to the corresponding memory location-C/C++ do not provide bounds checking, that is there is no checking done, neither at compile time, nor at runtime that the array access you are doing is actually valid-which is one of the major reasons why arrays are nowadays commonly considered "evil".

HTH

##### Share on other sites
So I should kind of skim through arrays and not concentrate as much on them? What is their replacement?

##### Share on other sites
***I AM WRONG***

Sorry, I didn't look at your code (carefully enough).

##### Share on other sites
Oh ok.

So in reality it is setting the very first array (0) to 75?

##### Share on other sites
...actually, I am not wrong-I looked only at the wrong posting ;-)
and no, arrays are crucial for your understanding of C/C++, there are ways to avoid such pitfalls.

##### Share on other sites
Also, why do multidimensional arrays ready (y,x), is that not a bit backwards?

And yet another question. :) Would arrays be the things used when a Player implements his/her name in a video game, as in Please Enter Your Name:

##### Share on other sites
Quote:
 Original post by GallivanAlso, why do multidimensional arrays ready (y,x), is that not a bit backwards?

X and Y are your interpretation. Multidimensional arrays have multiple dimensions: the name you give to dimensions is your choice only.

Quote:
 And yet another question. :) Would arrays be the things used when a Player implements his/her name in a video game, as in Please Enter Your Name:

A string would be a far better choice.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634049
• Total Posts
3015239
×