Jump to content
  • Advertisement
Sign in to follow this  
Gallivan

Arrays

This topic is 4412 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

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Gallivan
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?


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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Gallivan
Also, 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!