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.
[11:45 pm]
ClansAreForGays -- Anyone wanna played Skewed StarCraft?
[2026-4-14. : 12:07 am]
Vrael -- NudeRaider
NudeRaider shouted: Vrael ranting still is though
you're a gentleman and a scholar, thank you
[2026-4-13. : 10:07 pm]
NudeRaider -- ya why phone people when you can just write letters
[2026-4-13. : 9:37 pm]
IskatuMesk -- I have never and will never own a phone
[2026-4-13. : 9:15 pm]
NudeRaider -- Vrael ranting still is though
[2026-4-13. : 9:14 pm]
ClansAreForGays -- anticapitalism isnt edgy anymore
[2026-4-13. : 3:31 pm]
Vrael -- it only costs 50% of my post-tax salary for life and in return I get to also become a drone whose sole purpose is CAPITALISM
[2026-4-13. : 3:30 pm]
Vrael -- pssht, you're still using a phone? I just record 100% of my life using my ElonBrainChip
[2026-4-13. : 2:13 pm]
NudeRaider -- bro I don't go anywhere without my phone to record anything significant
[2026-4-13. : 1:28 pm]
Vrael -- Zoan
Zoan shouted: not if u wer there
id say even if you were there its tricky, human memory can be very faulty
Please log in to shout.


Members Online: Moose