What does ++ mean?

This is a discussion on What does ++ mean? within the C++ Programming forums, part of the General Programming Boards category; I trying to understand the code statements below, but I'm unsure as to what the ++ means. Code: float x ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    197

    What does ++ mean?

    I trying to understand the code statements below, but I'm unsure as to what the ++ means.
    Code:
    float x = *w++;
    *z++ = y;
    Does the variable x hold the value that is next to the value pointed to by *w? Does it following then that the value next to the value pointed to by *z is equal to y?

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    the ++ increases the variables value by 1. It is just like var = var+1; just made short
    My Website
    010000110010101100101011
    Add Color To Your Code!

  3. #3
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    c++
    c = c + 1
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  4. #4
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    c += 1
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    w and z are (probably) pointers in that code. The ++ means that they point to the next object in memory. It is a mechanism for going through an array.

    *w++ and *++w are different, even though both move the pointer value. The first returns the original pointer value, and when the statement has completed the pointer is incremented. That means in your example above that x gets the value w was pointing to before the ++ part. If it had been float x = *++w;, then w would have been incremented first, and then x would have gotten the value pointed to by w after the increment.

    Same goes for the second part. Because it is *z++, the value originally pointed to by z gets updated to y, and then the z pointer is incremented to the next element in the array.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Does the variable x hold the value that is next to the value pointed to by *w?
    It could depending on the precedence rules, i.e. which of the operator * or ++ gets applied first. Looking at an operator precedence table, ++ has a higher precedence. Since apparently 'w' is a pointer, ++ will move the pointer to the next element.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Operator precedence doesn't matter in determining whether the value x is set to is the originally pointed to value or the value pointed to after the increment. It only determines whether the pointer is incremented or the value pointed to is incremented. Since ++ has precedence, the pointer is incremented.

    What matters as to what value x gets is whether it is prefix or postfix. In this case it is postfix, so x gets the originally pointed to value, and the pointer is incremeneted afterwards.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    c++ adds 1 to c

    c =+ 1 means c= c + 1

  9. #9
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    I was going to reply that. But I decided to test, because I'm a little unclear on the subject in reference to pointers. I really need to read a reference.

    Anyway, I'm not sure how its happening. But it appears *z++ will change y to point to the next (or previous, can't tell) value in memory, and then you would be assigning the value pointed to by z to w. The thing is that the assignment doesn't carry through when its in one statement for some reason. However if you split the statement up it will work. Which is almost exactly what the OP asked if it was doing.

    Pointers just became fun.

    Code:
    #include <iostream>
    
    int main()
    {
        float a = 10.0;
        float b = 5.0;
        float c = 2.5;
    
        float* y = &c;
    
        std::cout << *y << std::endl; //Output: 2.5
    
        *y--; //no longer points to c, and now points to undefined
        std::cout << *y << std::endl; //Output: something x10^-39
        
        *y++; //no longer points to undefined, and now points back to c
        std::cout << *y << std::endl; //Output: 2.5
        
        *y++; //no longer points to c, and now points to b
        std::cout << *y << std::endl; //Output: 5.0
        
        *y++; //no longer points to b, and now points to a
        std::cout << *y << std::endl; //Output: 10.0
    
        ++*y; //increments the value of pointed to by the pointer
        std::cout << *y << std::endl; //Output: 11.0
    
        ++*y; //increments the value of pointed to by the pointer
        std::cout << *y << std::endl; //Output: 12.0
    
        --*y; //decrements the value of pointed to by the pointer
        std::cout << *y << std::endl; //Output: 11.0
    
        *y--; //no longer points to a, and now points to b
        *y = c; //change the value pointed to by the pointer (b) to c
        std::cout << b << std::endl; //Output: 2.5
    
        *y++ = a; //no longer points to b, and now points back to a, and attempt to change the value pointed to by the pointer (b) to c
        std::cout << b << std::endl; //Output: 5.0 (should output 2.5 because its being assigned c's value, but outputs a's value of 11.0)
    
        std::cin.get();
    }
    Edit: Looks like 7stud and Daved explained it. But still, code to match here.

    Edit: I understand now. I forgot. The reason *y++ = a; doesn't work is because it increments the pointer to the next place after the assignment.
    Last edited by Dae; 11-30-2005 at 03:08 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  10. #10
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    Quote Originally Posted by rodrigorules
    c++ adds 1 to c

    c =+ 1 means c= c + 1
    well thats the same, but i think it shouldn't be =+, it should +=;
    And what does
    ++c;
    means?
    I know it's possible.
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  11. #11
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by ElastoManiac
    well thats the same, but i think it shouldn't be =+, it should +=;
    And what does
    ++c;
    means?
    I know it's possible.
    It meants c is incremented before it returns its value.

    int c = 5, a = 0;

    a = ++c; //would be 6
    a = c++; //would be 5
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Dae, you shouldn't be using ++ or -- on y in your example anywhere. It's all undefined. It worked in some cases for you because of the way memory is laid out on your current platform.

    Try making an array and then point y at the array. Then you can see a better (and more legal) version of what's going on.

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    and just to make this thread even more confusing (*x)++ will incremenet the value pointer to by x rather than x pointer itself.
    Last edited by Ancient Dragon; 11-30-2005 at 03:35 PM.

  14. #14
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    Quote Originally Posted by Ancient Dragon
    and just to make this thread even more confusing (*x)++ will incremenet the value pointer to by x rather than x pointer itself.
    interesting
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  15. #15
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by ElastoManiac
    well thats the same, but i think it shouldn't be =+, it should +=;
    And what does
    ++c;
    means?
    I know it's possible.
    think of it like this
    var++
    is equivalent to the following function
    Code:
    int Postfix(int& var)
    {
        int temp = var;
        var = var + 1;
        return temp;
    }
    whereas
    ++var
    is equivalent to the following function
    Code:
    int Prefix(int& var)
    {
        var = var + 1;
        return var;
    }
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Page 1 of 2 12 LastLast
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