Arrays

Hi,
in my program i have a array called inventory, see below:

inventory [numItems++] = "sword";
inventory [numItems++] = "armor";
inventory [numItems++] = "shield";

i have set the limit of inventory to 10

as you can see i have put:

= "sword";
= "armor";
= "shield";

Now, because i have used "=" and not "+=" surely everytime ive put = something it overwrites the previous value, or because its an array does it automatically add each time, even tho i aint used += ???
Sure, it overwrites the previous value.
inventory might be an array, but inventory [numItems++] is not. It's a string object.
What's inventory[]'s type?

Using = for a string or char * overwrites the previous value in the spot (but it's important to note that you're ++ing a counter before you write into the spot indicated), yes, but += even though it seems intuitive doesn't append when the left-hand argument is a char* (but it works intuitively for std::strings).

-Albatross
Last edited on
hmm, could you explain to me whats going on here:

const int MAX_ITEMS = 10;
string inventory [3];

int numItems = 0;

inventory [numItems++] = "sword";
inventory [numItems++] = "armor";
inventory [numItems++] = "shield";

The thing i dont understand is how by putting [numItems++] it makes it add, instead of replace, because "numItems" does not directly communicate with the inventory array, its been made just so i know how many values are in inventory.

I think???
I get the feeling that you might get an out of bounds error if you run this, or maybe this is a slow day. numItems starts at 0, and array indexes run from 0 to n-1 where n is their size, but the counter is incremented before the write, so you end at n, not n-1.

-Albatross
thanks for the reply, but...

erm, huh?
ah i understand it, i have two questions tho:

1. does the postfix increment operator always get calculated last in a statement, i asking this because im my book it says " because i use to postfix increment operator, numitems gets incremented after the assignment to the array".

2. everytime i put a string name followed by "[..something..]" does it ALWAYS refer to the index number of the string??
1. No. But it will always evaluate to the value it had before the increment, which is what matters.
2. inventory is not a string, it is an array of strings. inventory[0] refers to the first string in the array, inventory[1] to the second etc.
ok, so yeah [] does refer to the index.

also, so ok, the postfix increment operator doesnt technically get calculated last, but its effect it has APPEARS as if it gets calculated last,and thats just the way it works, right?
More or less. The C++ standard has no explicit rules about when the incrementing is actually performed between two sequence points.
That means that the value of i after executing
1
2
int i=2;
i=i++;

is undefined. Your compiler could first take the value of i, increment it and then assign the previous value to i (that means i would still be 2) or it could take the value of i, assign it to i and then increment i, so i would be 3.
You might get different results with different compilers.
ah ok, thanks
Topic archived. No new replies allowed.