About simple confusion of array incrementation

Discussion in 'C++' started by sam, Jun 26, 2007.

  1. sam

    sam Guest

    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
    sam, Jun 26, 2007
    #1
    1. Advertising

  2. 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 26, 2007
    #2
    1. Advertising

  3. sam

    sam Guest

    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!!!!!!!!!
    sam, Jun 26, 2007
    #3
  4. 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
    Robert Bauck Hamar, Jun 26, 2007
    #4
  5. sam

    Andre Kostur Guest

    sam <> wrote in news:1182868592.336797.296630
    @x35g2000prf.googlegroups.com:

    > 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 }.
    Andre Kostur, Jun 26, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf Hildebrandt

    Re: Nested For Loop incrementation

    Ralf Hildebrandt, Jul 2, 2003, in forum: VHDL
    Replies:
    3
    Views:
    3,446
    Aaron McFarland
    Jul 3, 2003
  2. everett

    Variable name incrementation

    everett, Feb 13, 2004, in forum: C++
    Replies:
    3
    Views:
    414
    Jon Bell
    Feb 13, 2004
  3. Petterson Mikael

    Confusing incrementation

    Petterson Mikael, Sep 6, 2006, in forum: XML
    Replies:
    0
    Views:
    376
    Petterson Mikael
    Sep 6, 2006
  4. Max

    Incrementation

    Max, Mar 23, 2007, in forum: Java
    Replies:
    3
    Views:
    345
  5. Replies:
    4
    Views:
    288
    santosh
    Apr 26, 2008
Loading...

Share This Page