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.
[08:51 pm]
l)ark_ssj9kevin -- Are you excited for Homeworld 3?
[08:44 pm]
l)ark_ssj9kevin -- Hi Brusilov
[2024-5-12. : 4:35 pm]
O)FaRTy1billion[MM] -- Brusilov
Brusilov shouted: Hey, what happened to EUDDB? Is there a mirror for it somewhere? Need to do a little research.
my server that was hosting it died
[2024-5-10. : 8:46 pm]
NudeRaider -- Brusilov
Brusilov shouted: Hey, what happened to EUDDB? Is there a mirror for it somewhere? Need to do a little research.
https://armoha.github.io/eud-book/
[2024-5-10. : 8:36 am]
Brusilov -- Hey, what happened to EUDDB? Is there a mirror for it somewhere? Need to do a little research.
[2024-5-09. : 11:31 pm]
Vrael -- :wob:
[2024-5-09. : 8:42 pm]
Ultraviolet -- :wob:
[2024-5-08. : 10:09 pm]
Ultraviolet -- let's fucking go on a madmen rage bruh
[2024-5-08. : 10:01 pm]
Vrael -- Alright fucks its time for cake and violence
[2024-5-07. : 7:47 pm]
Ultraviolet -- Yeah, I suppose there's something to that
Please log in to shout.


Members Online: NudeRaider