About simple confusion of array incrementation

S

sam

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
Thanks in advance
 
V

Victor Bazarov

sam said:
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
 
S

sam

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
Please Help me here?_?_?_
I am really confused!!!!!!!!!
 
R

Robert Bauck Hamar

sam said:
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.
 
A

Andre Kostur

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
Please Help me here?_?_?_
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 }.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top