this post was submitted on 09 May 2024
12 points (92.9% liked)

Programmer Humor

32380 readers
425 users here now

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

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] Tolookah@discuss.tchncs.de 1 points 6 months ago (3 children)

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

[–] lseif@sopuli.xyz 1 points 6 months ago
[–] Tyoda@lemm.ee 0 points 6 months ago* (last edited 6 months ago) (2 children)

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

[–] fluckx@lemmy.world 0 points 6 months ago* (last edited 6 months ago) (1 children)
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?

[–] SpaceNoodle@lemmy.world 0 points 6 months ago* (last edited 6 months ago) (1 children)

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

[–] fluckx@lemmy.world 0 points 6 months ago (1 children)

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 1 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.

[–] xmunk@sh.itjust.works -1 points 6 months ago (1 children)

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

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

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

[–] marcos@lemmy.world 0 points 6 months ago* (last edited 6 months ago) (1 children)

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 1 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.