Arrays
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?
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.
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.
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.
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
So I should kind of skim through arrays and not concentrate as much on them? What is their replacement?
***I AM WRONG***
Sorry, I didn't look at your code (carefully enough).
Please disregard my answer or delete it :-)
Sorry, I didn't look at your code (carefully enough).
Please disregard my answer or delete it :-)
...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.
and no, arrays are crucial for your understanding of C/C++, there are ways to avoid such pitfalls.
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:
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:
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement