array[n++];

This is a discussion on array[n++]; within the C++ Programming forums, part of the General Programming Boards category; Whenever I see something like array[n++], I get a bit confused...because I don't really see it that often. Does that ...

  1. #1
    People Love Me
    Join Date
    Jan 2003
    Posts
    412

    array[n++];

    Whenever I see something like array[n++], I get a bit confused...because I don't really see it that often. Does that kind of syntax come in handy for anything? It's just a way to compound:
    Code:
    array[n]=n;
    n++;
    Right? I see it used a lot in loops too, and it kind of throws me off. I've seen it used in a SetString() function too.

    And while I'm at it...I should ask: if you wish to pass an array into a function (which I don't think you can do), you would just pass the address of the first element as a pointer, and the number of elements there are in total as an int, right?

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    No, you're wrong,
    array[n++] is just short way of writting... You need to have real code to understand what it really means. Yes it is often used in loops. For example

    Code:
    int n =0;
    int array[10];
    // some code
    while (cin >> array[n++]);
    n--;
    cout << "You enetered "<<n<< " int numbers."<<endl;
    or you can just write
    Code:
    while (cin >> array[n])
    {
    n++;
    }
    // in this case you don't need line n--
    Arrays are passed to functions only by reference i.e. through pointer to the first element...
    When you passed arrays as function arguments usually you need to provide information about array length as well (except for C-style string, where you can determine length with '\0')

    - Micko
    Last edited by Micko; 07-05-2005 at 02:21 PM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Quote Originally Posted by Micko
    No, you're wrong
    Umm.....how? There's nothing wrong with what he said at all. And your post....you just reiterated what he said, especially with the part about passing arrays.
    Code:
    void function(void)
     {
      function();
     }

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    Ya thats correct
    Code:
    array[n++] = number;
    
    // is the same as...
    
    array[n] = number;
    n++;

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    This part of what Krak said was wrong:

    array[n++]

    is not the same as

    array[n] = n;
    n++;

    The first bit of code can be used in many different situations, and most if not all of them are different than the second piece of code.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Specifically, this is what was wrong with the statement:
    Code:
    array[n++]
    This by itself does nothing at all other than increment the variable n. What you meant to say I believe was something like this:
    Code:
    array[n++] = foo;
    The above is identical to this:
    Code:
    array[ n ] = foo;
    n++;
    To further the point, this means:
    Code:
    array[n++]
    "at array element n, do whatever, then increment n"


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    However,
    Code:
    //prefix
    array[++n] = var;
    is different from
    Code:
    //postfix
    array[n++] = var;
    The first one (pretfix) increments n, and then assigns var the value or array[n]. In other words, it's like
    Code:
    n++;
    array[n] = var;
    The other sample, postfix, is like this code:
    Code:
    array[n] = var;
    n ++;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    I knew that.
    I don't really think there's any real reason to compound the two statements like that. I think it really takes readability away from code sometimes.

Popular pages Recent additions subscribe to a feed

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