postfix in array index

This is a discussion on postfix in array index within the C++ Programming forums, part of the General Programming Boards category; Hello. I am trying to convert some c++ to another more basic language and i just wanted to clarify something. ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    2

    postfix in array index

    Hello.
    I am trying to convert some c++ to another more basic language and i just wanted to clarify something.

    array[ind++]

    Does this code increment the array index before or after it is used.

    Like if i did the following:
    ind = 3

    a = array[ind++]

    would a be assigned the value in element 3 or element 4?

    I am still learning c++ and have never seen this before so any comments would be much appreciated.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,475
    a would be assigned the value in element 3. ind would only then be incrememented.

    However... it important to understand one thing; When is ind really incremented?

    You have to understand what lvalues and rvalues are.

    Shortly, an lvalue is an expression that can appear both in the left and the right side of an assigment. While a rvalue can only appear on the right-hand side of an assignment.

    Code:
    int a = 12;    // a is an lvalue. 12 is a rvalue. You cannot write 12 = a;
    int b = a;       // b and a are lvalues. a, being an lvalue, can also be used on the right-hand side.
    int c = a + b * 12;    // The expression a + b * 12 yields a rvalue. You cannot write a + b *12 = c;
    So the big difference is this...

    A prefixed increment or decrement operator (++ind) yields a lvalue as a result. It returns the object itself after applying the operator.

    A postfixes increment or decrement operator (ind++) yields a rvalue. It returns a copy of the unchanged value of the object before it was incremented.

    Both prefix and suffix versions of ind you have, happen when ind is being evaluated. However what is really different is what is returned from them.
    Last edited by Mario F.; 07-10-2006 at 07:33 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    2
    Thanks for such a quick and detailed explanation, it’s got me out of a hole I had been wallowing in for some time :0)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C] Sorting an array and preserving the original index
    By frodo_jedi in forum C Programming
    Replies: 10
    Last Post: 04-06-2009, 06:51 AM
  2. Reversing character array without accessing thro' index.
    By Roaring_Tiger in forum C Programming
    Replies: 8
    Last Post: 08-28-2004, 10:52 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21