Results 1 to 3 of 3

Thread: Using ++

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Using ++

    I have this line of C code

    Code:
    ppi = &pcon->argpi[co][pcon->argcpi[co]++];

    PPI ppi; //a pointer to a structure
    PCON pcon; //s a pointer to a structure
    argpi is a 2d array of type PI
    argcpi is a 1d integer array
    co is an integer always 1

    My question is what does the ++ at the end increment?

    I'm assuming it increments the value in the argcpi[co] integer array


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,468

    Re: Using ++

    `++` is the increment operator, i.e. it adds 1 to its operand. It can be a pre-increment, i.e. increment first and then use the value, e.g. ++n, or it can be a post-increment, i.e. use the value first and then increment, e.g. n++.

    It's a while since I've used C/C++ so I don't remember the full operator precedence but I think you'll find that the indirection operator, i.e. `->` takes precedence over the increment operator. That would mean that this:
    Code:
    pcon->argcpi[co]++
    would mean use the value of `pcon->argcpi[co]` and then increment the value of that expression. Incrementing indexes like that is common in C/C++ because it allows you to use a value and, in the same line, make that value ready to be used in a subsequent iteration of a loop or the like.

  3. #3
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    679

    Re: Using ++

    The order of precedence is
    [] ->
    ++
    So
    Code:
    pcon->argcpi[co]++
    becomes
    Code:
    ((pcon->argcpi)[co])++
    the [] and => associate left to right
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C++17 Compiler: Microsoft VS2019 (16.7.4)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width