• Advertisement
Sign in to follow this  

Arrays

This topic is 4169 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
So I should kind of skim through arrays and not concentrate as much on them? What is their replacement?

Share this post


Link to post
Share on other sites
***I AM WRONG***

Sorry, I didn't look at your code (carefully enough).
Please disregard my answer or delete it :-)

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
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 first element in the array starting at the address billy is set to the value of the variable a:

int billy[5]; // billy = [#UND, #UND, #UND, #UND, #UND]
int a = 4;
billy[0] = a; // billy = [4, #UND, #UND, #UND, #UND]


Quote:
The second one I do not understand.

a holds a numeric value, which is used to subscript the array. The resulting element is assigned the value 75:
billy[a] = 75;      // billy = [4, #UND, #UND, #UND, 75]

Keep in mind that for type array[N], the highest index is N-1. If a been assigned a value of 5 or greater, the code above would have been an error.

In my illustration of the array contents, #UND is used to indicate that the value at that index is undefined. C and C++ do not initialize variables to known values by default.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gallivan
Except for global and static members correct?


The sentence above is lacking two essential elements: a verb, and a subject.

Share this post


Link to post
Share on other sites
Quote:
Original post by Gallivan
Except for global and static members correct?

Nope. C and C++ do not initialize globals. Static (class, as opposed to instance) members of an integral type will be initialized to 0.

Share this post


Link to post
Share on other sites
Quote:
Original post by win_crook

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



Is that right? :)
billy[5] doesn't exist!

Share this post


Link to post
Share on other sites
Quote:
Original post by win_crook

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


It is dangerous to make even such small errors on the beginners forum, someone just starting out might memorize it and confuse things later on even when he knows how that it's wrong

int billy[5];

reserves place for an array holding 5 integer numbers on the stack.
That means you can access them individually by calling billy[0] to billy[4].
Calling billy[5] is undefined behaviour, you are calling a value of an address that you have not reserved for billy, that means you don't really know what resides in that place.

[Edited by - kiome on September 21, 2006 4:38:27 PM]

Share this post


Link to post
Share on other sites
I removed your quote, I wasn't sure if your response was a question or a correction.

Share this post


Link to post
Share on other sites
Maybe I'm wrong here, but I rather be reminded or better informed than to not state it.

Isnt:

billy[0] an error, even if you give it a data type?

I could of sworn I remember reading that even though the array starts at 0 you still need to have a value bigger than 0 in order for N-1 to work.

-PB

Share this post


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

  • Advertisement