Staredit Network > Forums > Technology & Computers > Topic: C++ Question
C++ Question
Jan 16 2008, 10:09 pm
By: fatimid08  

Jan 16 2008, 10:09 pm fatimid08 Post #1



I'm a bit confused as to the interpretation of the following snippet:

*somepointer++ = somevalue

Does it increment the pointer and then assigns the value to what it points to, or does it assign the value to what it points to and then increments it?



None.

Jan 16 2008, 10:24 pm Falkoner Post #2



somevalue will get the value of somepointer + 1.



None.

Jan 16 2008, 11:10 pm Laser Dude Post #3



I just tested it (it took 5 minutes). The compiler outputs an error because, quote "Test.cpp:7: error: invalid lvalue in assignment". Here's the code snippet I tested:

Code
#include<iostream>

int main()
{
    int x = 0;

    x++ = 2;

    std::cout << x << std::endl;
}




None.

Jan 16 2008, 11:46 pm fatimid08 Post #4



x should be a pointer to another variable, not just a variable, else it doesn't make sense (and doesn't work, from your experiences).

And Falkoner, what on earth are you trying to say? If it means what I think it means, it's horribly wrong (the right hand side of the expression gets something assigned to????), and if not, well you should change your wording. And if it was sarcasm or a joke, well it's not a funny one.



None.

Jan 16 2008, 11:48 pm Esponeo Post #5



Falkoner and Laser Dude are retards who don't know what the fuck they are talking about.

Quote
void main()
{
int value = 10;
int *pointer = &value;
cout << (*pointer);
*pointer++ = 4;
cout << (*pointer);
}

As far as I can tell the address in memory the pointer is aimed at is what is being increased by the ++. Which is exactly what should happen, check out this priority table:
http://paowang.com/blog/hufey/archives/010535.html

So its basically a horrible memory leak, you're altering some address in memory you don't know jack about.



None.

Jan 17 2008, 12:14 am fatimid08 Post #6



So it's assignment first, increment after. Thanks.

The code is actually looping through known memory in compression/decompression stuff I'm porting from C++ to VB.Net as part of the cross-os MPQ library I'm working on, so it's not some random memory.



None.

Jan 17 2008, 8:09 pm ShadowFlare Post #7



If ++ (or --) is after the variable name, it always takes effect after the statement executes. So if you use variablename++ in an expression, the value used in the expression is the value before the increment. If you have ++ (or --) before the variable name, then it does the operation before reading the value of the variable. For example:

x = 3;
y = 2 + x++;

After those two statements, x = 4 and y = 5 because x was incremented after the second statement finished. This is how the post-increment operator works (variable++). An example of the pre-increment operator (++variable):

x = 3;
y = 2 + ++x;

Same as before, x = 4 after the second statement, but this time y = 6 because x was incremented before the calculation.



None.

Jan 17 2008, 9:12 pm cheeze Post #8



Anyone who doesn't know what a pointer is should not be posting in this. Esponeo is right anyway.



None.

Jan 18 2008, 2:07 am ShadowFlare Post #9



Oh, there was one more thing I did forget to mention.

When using ++ (or --) with dereferenced pointers, it applies it to the pointer, not the memory location, unless you put the *variablename in parentheses and the ++ (or --) outside. For example,

*pointername++ increments pointername after the statement that used it; it does not increment *pointername. To increment *pointername this way, you must use (*pointername)++.

Yes, it can be kind of confusing, but that's just the way it is.

Post has been edited 1 time(s), last time on Jan 18 2008, 2:13 am by ShadowFlare.



None.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[05:02 am]
Oh_Man -- whereas just "press X to get 50 health back" is pretty mindless
[05:02 am]
Oh_Man -- because it adds anotherr level of player decision-making where u dont wanna walk too far away from the medic or u lose healing value
[05:01 am]
Oh_Man -- initially I thought it was weird why is he still using the basic pre-EUD medic healing system, but it's actually genius
[03:04 am]
Ultraviolet -- Vrael
Vrael shouted: I almost had a heart attack just thinking about calculating all the offsets it would take to do that kind of stuff
With the modern EUD editors, I don't think they're calculating nearly as many offsets as you might imagine. Still some fancy ass work that I'm sure took a ton of effort
[12:51 am]
Oh_Man -- definitely EUD
[09:35 pm]
Vrael -- I almost had a heart attack just thinking about calculating all the offsets it would take to do that kind of stuff
[09:35 pm]
Vrael -- that is insane
[09:35 pm]
Vrael -- damn is that all EUD effects?
[2024-5-04. : 10:53 pm]
Oh_Man -- https://youtu.be/MHOZptE-_-c are yall seeing this map? it's insane
[2024-5-04. : 1:05 am]
Vrael -- I won't stand for people going around saying things like im not a total madman
Please log in to shout.


Members Online: lil-Inferno, Ultraviolet