459
got him (lemy.lol)
submitted 6 months ago by ngn@lemy.lol to c/programmerhumor@lemmy.ml
you are viewing a single comment's thread
view the rest of the comments
[-] Tolookah@discuss.tchncs.de 38 points 6 months ago

I didn't know why, but *++p bugs me

[-] Tyoda@lemm.ee 29 points 6 months ago* (last edited 6 months ago)

Perhaps *(p += 1) will be to your liking?

[-] xmunk@sh.itjust.works 15 points 6 months ago

Much better... but can we make it *((void*)(p = p + 1))?

[-] shrugal@lemm.ee 16 points 6 months ago

How about some JavaScript p+=[]**[]?

[-] Faresh@lemmy.ml 3 points 6 months ago

Why are you casting to void*? How is the compiler supposed to know the size of the data you are dereferencing?

[-] xmunk@sh.itjust.works 3 points 6 months ago* (last edited 6 months ago)

This would probably cause a compiler error....

But assuming it doesn't the context is p_ch = the bits above... the code declaring p_ch isn't shown but I'm guessing that the value here is actuality a pointer to a pointer so nothing illegal would be happening.

Lastly... C++ is really lacking in guarantees so you can assign a char to the first byte of an integer - C++ doesn't generally care what you do unless you go out of bounds.

The reason I'm casting to void* is just pure comedy.

[-] fluckx@lemmy.world 11 points 6 months ago* (last edited 6 months ago)
p = 1

x = ++p
// x = 2
// p = 2
p = 1
x  = p++
// x = 1
// p = 2

++p will increase the value and return the new value

p++ will increase the value and return the old value

I think p = p + 1 is the same as p++ and not as ++p. No?

[-] Tyoda@lemm.ee 10 points 6 months ago* (last edited 6 months ago)

In C an assignment is an expression where the value is the new value of what was being assigned to.

In a = b = 1, both a and b will be 1.

a = *(p = p + 1)

is the same as

p += 1
a = *p

, so ++p.

[-] fluckx@lemmy.world 1 points 6 months ago

What I meant was:

In the screenshot it said x = *(++p) and iirc that is not the same as saying x = *(p++) or x = *(p += 1)

As in my example using ++p will return the new value after increment and p++ or p+=1 will return the value before the increment happens, and then increment the variable.

Or at least that is how I remember it working based on other languages.

I'm not sure what the * does, but I'm assuming it might be a pointer reference? I've never really learned how to code in c or c++ specifically. Though in other languages ( like PHP which is based on C ) there is a distinct difference between ++p and (p++ or p+= 1)

The last two behave the same. Though it has been years since I did a lot of coding. Which is why I asked.

I'll install the latest PHP runtime tonight and give it a try xD

[-] SpaceNoodle@lemmy.world 3 points 6 months ago* (last edited 6 months ago)

(p += 1) resolves to the value of p after the incrementation, as does ( p = p + 1).

[-] fluckx@lemmy.world 2 points 6 months ago

Yes.

p++ == p+= 1 == p = p + 1 are all the same if you use it in an assignment.

++p is different if you use it in an assignment. If it's in its own line it won't make much difference.

That's the point I was trying to make.

[-] SpaceNoodle@lemmy.world 5 points 6 months ago

No.

++p returns incremented p.

p += 1 returns incremented p.

p = p + 1 returns incremented p.

p++ returns p before it is incremented.

[-] fluckx@lemmy.world 3 points 6 months ago

Right. So i had them the other way around. :D

Thanks for clarifying.

[-] lseif@sopuli.xyz 13 points 6 months ago
[-] marcos@lemmy.world 8 points 6 months ago* (last edited 6 months ago)

That *++ operator from C is indeed confusing.

Reminds me of the goes-to operator: --> that you can use as:

while(i --> 0) {
[-] letsgo@lemm.ee 15 points 6 months ago

That's not a real operator. You've put a space in "i--" and removed the space in "-- >". The statement is "while i-- is greater than zero". Inventing an unnecessary "goes to" operator just confuses beginners and adds something else to think about while debugging.

And yes I have seen beginners try to use <-- and --<. Just stop it.

[-] marcos@lemmy.world 1 points 6 months ago

The sheer number of people that do not expect a joke on this community... (Really, if you are trying to learn how to program pay attention to the one without the Humor on the name, not here.)

Well, I guess nobody expects.

[-] SpaceNoodle@lemmy.world 1 points 6 months ago

Where do you think we are?

this post was submitted on 09 May 2024
459 points (92.4% liked)

Programmer Humor

32410 readers
465 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS