Velocity Reviews > C++ > About simple confusion of array incrementation

# About simple confusion of array incrementation

sam
Guest
Posts: n/a

 06-26-2007
hI,
I am little confused here
See
i have
int wordlen=10;

when int s[] is array

s[wordlen-1-1]++;
whats the meaning of this
can it affects the number which are in array.
or it can just shifts from say 2 numbers(which are minused here after
each incrementation)
what i want to ask is ? is it shifting 2 numbers or it is minusing 2
numbers in array.
means can it affecting the numbers in array.

See why I am confused is the real question is here?
lets take wordlen=20 ok
and if we minusing -1-1 means -2 each time then the wordlen will be 18
is it means wordlens value will be changes to 18
or the value of array will be changes to 18 because in next iteration
(lets take more than one iteration for s[wordlen-1-1]++)
value of wordlen will be remain same because
at the start we declare
int wordlen=20;
i am quite confused but i think some you understand
in SHORT MY QUESTION IS
is incrementation affecting the value of wordlen or it can affect the
value in array means not wordlen particularly
PLease give me some explanation and make the way clear

Victor Bazarov
Guest
Posts: n/a

 06-26-2007
sam wrote:
> hI,
> I am little confused here
> See
> i have
> int wordlen=10;
>
> when int s[] is array
>
> s[wordlen-1-1]++;
> whats the meaning of this

If the line above is a question (I am used to seeing question marks
at the end of questions, but that's just me), then the answer is

post-increment the element at the index (wordlen-2) of the array
's' and discard the return value.

> can it affects the number which are in array.

It should does (given that the array has more than 8 elements).

> or it can just shifts from say 2 numbers(which are minused here after
> each incrementation)

Shifts? I am not sure what you mean.

> what i want to ask is ? is it shifting 2 numbers or it is minusing 2
> numbers in array.

I have no idea what you mean here.

> means can it affecting the numbers in array.

It can affecting.

> See why I am confused is the real question is here?

Yes, I sure have.

> lets take wordlen=20 ok

ok, 20

> and if we minusing -1-1 means -2 each time then the wordlen will be 18

No, the index would be 18. 'wordlen' will stay 20.

> is it means wordlens value will be changes to 18

No, it doesn't mean that.

> or the value of array will be changes to 18 because in next iteration

No, it doesn't mean that either. It means the element of the 's'
array _at_ the position 18 would be incremented.

> (lets take more than one iteration for s[wordlen-1-1]++)

lets

> value of wordlen will be remain same because
> at the start we declare
> int wordlen=20;

No, not because we declare. Because the expression does not change the
value of 'wordlen'.

> i am quite confused but i think some you understand

You are, yes. I understand.

> in SHORT MY QUESTION IS
> is incrementation affecting the value of wordlen or it can affect the
> value in array means not wordlen particularly

Correct. The incrementation affecting the value in array.

> PLease give me some explanation and make the way clear

Indexing operator is applied first. It returns [a reference to] the
element in the array. Then the post-increment is applied to that
element. The element is incremented.

V
--

sam
Guest
Posts: n/a

 06-26-2007
HI,
My friend just take a look at what i am saying
s[worllen-1-1]++;
IN THIS VALUE OF ARRAY WILL BE 18
IN next iteration again value of wordlen will be worldlen(because we
are not changing the value of wordlen in iteration)
means the value of wordlen will remain 20 in next iteration also and
we are minusing 2 from this , then we get the same value inside array
that is 18
SEE HOW THIS POSSIBLE EVERY TIME IN NEXT ITERATION ALSO THE VALUE IN
ARRAY REMAIN SAME AND SO AFTER INCREMENTING SUCH ARRAY ALSO THE VALUE
CAN REMAIN SAME FOR THE NEXT ITERATION ALSO
So
This concept of mine(may be MISCONCEPT) BUT I want make clear what is
boiling in mind
I am really confused!!!!!!!!!

Robert Bauck Hamar
Guest
Posts: n/a

 06-26-2007
sam wrote:

> HI,
> My friend just take a look at what i am saying
> s[worllen-1-1]++;
> IN THIS VALUE OF ARRAY WILL BE 18

No, the value of the _index_ will be 18.

in
s[wordlen-1-1]++;

s is the array
wordlen-1-1 is the index
s[wordlen-1-1] is an element of the array.

Since wordlen == 20, the index is 18, and you are referencing the 19th
element.

> IN next iteration again value of wordlen will be worldlen(because we
> are not changing the value of wordlen in iteration)

Yes.

> means the value of wordlen will remain 20 in next iteration also and
> we are minusing 2 from this , then we get the same value inside array
> that is 18

Yes.
wordlen-1
returns a temporary whose value is 19, and subtracting 1 from that temporary
will return a new temporary whose value is 18. The computations never alter
the value of wordlen.

> SEE HOW THIS POSSIBLE EVERY TIME IN NEXT ITERATION ALSO THE VALUE IN
> ARRAY REMAIN SAME AND SO AFTER INCREMENTING SUCH ARRAY ALSO THE VALUE
> CAN REMAIN SAME FOR THE NEXT ITERATION ALSO

No. The value of the index remains the same. The 19th element of the array,
and thus the array itself, is modified.

--
rbh

Andre Kostur
Guest
Posts: n/a

 06-26-2007
sam <(E-Mail Removed)> wrote in news:1182868592.336797.296630

> HI,
> My friend just take a look at what i am saying
> s[worllen-1-1]++;
> IN THIS VALUE OF ARRAY WILL BE 18
> IN next iteration again value of wordlen will be worldlen(because we
> are not changing the value of wordlen in iteration)
> means the value of wordlen will remain 20 in next iteration also and
> we are minusing 2 from this , then we get the same value inside array
> that is 18
> SEE HOW THIS POSSIBLE EVERY TIME IN NEXT ITERATION ALSO THE VALUE IN
> ARRAY REMAIN SAME AND SO AFTER INCREMENTING SUCH ARRAY ALSO THE VALUE
> CAN REMAIN SAME FOR THE NEXT ITERATION ALSO
> So
> This concept of mine(may be MISCONCEPT) BUT I want make clear what is
> boiling in mind
> I am really confused!!!!!!!!!

Unfortunately so are we. At least I'm not entirely sure what you are
attempting to do.

Let us assume an array of 3 items, and worllen is 3 (because I don't want
to write 20 array values every time....):

int worllen = 3;
int s[3];

s[0] = 0;
s[1] = 0;
s[2] = 0;

Also, let's use a loop of 10 iterations, performing your operation:

for (int i = 0; i < 10; ++i)
{
s[worllen - 1 - 1]++;
}

Since worllen isn't modified during this loop, the loop effectively
behaves as if you had written:

for (int i = 0; i < 10; ++i)
{
s[3 - 1 - 1]++;
}

And if we calculate the constant expression in there, you end up with
effectively the following loop:

for (int i = 0; i < 10; ++i)
{
s[1]++;
}

The array starts with the values: { 0, 0, 0 }.

After the first iteration, the array will contain: { 0, 1, 0 }.

After the second iteration, the array will contain: { 0, 2, 0 }.

.....

After the tenth iteration, the array will contain: { 0, 10, 0 }.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post HansWernerMarschke@web.de C Programming 4 04-26-2008 10:15 AM Max Java 3 03-23-2007 04:35 PM Petterson Mikael XML 0 09-06-2006 11:40 AM everett C++ 3 02-13-2004 07:12 PM Ralf Hildebrandt VHDL 3 07-03-2003 12:28 AM