this post was submitted on 13 Jul 2023
693 points (99.6% liked)

Programmer Humor

32371 readers
509 users here now

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

Rules:

founded 5 years ago
MODERATORS
 
top 31 comments
sorted by: hot top controversial new old
[–] agressivelyPassive@feddit.de 58 points 1 year ago (1 children)

That's actually a pretty good representation.

[–] SubArcticTundra@lemmy.ml 8 points 1 year ago

I think this graphic should be used on all CS courses from now on

[–] itsJoelleScott@lemmy.world 58 points 1 year ago (4 children)

H-how did this meme clear up a confusion I have with ** pointers after all these years?

They're just pointer pointers!

Dang, that is so cool C can do that.

[–] AlmightySnoo@lemmy.world 32 points 1 year ago (1 children)

pointer pointers

wait until you learn about template templates in C++

Are the templates in the room with us right now?

[–] shotgun_crab@lemmy.world 19 points 1 year ago* (last edited 1 year ago)

I know a guy who knows a guy who knows a guy

[–] mediocreatbest@lemmy.sdf.org 6 points 1 year ago

If you've never seen this before, I think it's transformative to how you read C/C++ declarations and clearer up a lot of confusion for me when I was learning.

https://cseweb.ucsd.edu/~gbournou/CSE131/rt_lt.rule.html

[–] b1_@kbin.social 2 points 1 year ago* (last edited 1 year ago)

Yeah, and I'm pointing to your pointer pointer and I'm a pointer pointer pointer. Or more accurately but less funny is that I'm pointing to your pointer-to-pointer and I'm a pointer-to-pointer-to-pointer.

[–] dandroid@dandroid.app 45 points 1 year ago (1 children)

I'm a simple man. I see Anya, I upvote.

[–] AlmightySnoo@lemmy.world 34 points 1 year ago (1 children)
[–] dandroid@dandroid.app 18 points 1 year ago (1 children)
[–] AlmightySnoo@lemmy.world 21 points 1 year ago (1 children)
[–] gravitas_deficiency@sh.itjust.works 7 points 1 year ago (1 children)

Damn, OP’s on point tonight

[–] clutchmatic@lemmy.world 5 points 1 year ago

No, he is on a pointer to a pointer to a point tonight

[–] Sebito@lemmy.ml 25 points 1 year ago

int** int* interesting

[–] MavTheHack@lemmy.fmhy.ml 14 points 1 year ago (7 children)

Wait we can have pointers to other pointers? Wouldn't that be redundant?

[–] AlmightySnoo@lemmy.world 32 points 1 year ago (2 children)

In CUDA, the corresponding malloc cannot return the pointer to the allocated memory as runtime CUDA functions all return error codes instead. So the only way to "return" the pointer then without a return statement is to have a pointer given to that function by address, which means that you'll have a pointer-to-pointer among its arguments.

Man, this is the type of interaction I used to love on Reddit, but haven’t seen in ages.

[–] abraham_linksys@sh.itjust.works 5 points 1 year ago (1 children)

So it's sort of like "proxying" through pointers to enforce memory isolation?

[–] AlmightySnoo@lemmy.world 11 points 1 year ago* (last edited 1 year ago) (1 children)

I'm not entirely sure what you mean by memory isolation here, but the basic idea is that if you have a pointer to something then you know where it is located in memory and you can write in it, that's the whole idea of passing by address in C.

Now pointers themselves are merely variables. Yes they have a special meaning, they "point" to something and you can dereference them with the * operator, but at the end of the day they're still variables. They have a physical existence in memory or CPU registers, and their content is simply the address to which you want to point. Once you accept this, then the idea of the address of a pointer (ie the location of the variable you're calling "pointer", and not the address it contains) is not strange anymore and you can perfectly have a pointer-to-pointer in order to, among other things, pass pointers by address.

[–] Iridium@lemmy.world 7 points 1 year ago (2 children)

that’s the whole idea of passing by address in C

Wait stop, so in other languages like C#, when you pass a variable into a function “by reference” is that just passing the pointer to the variable?

Have I been baited into using pointers my whole life?

[–] olorin99@kbin.social 10 points 1 year ago

Yes passing "by reference" is essentially the same as "by pointer" but with some syntactical sugar to make it easier to work with.

[–] pazukaza@lemmy.ml 7 points 1 year ago* (last edited 1 year ago)

In C# it is different.

In C if I give you a pointer to a memory address, you can totally overwrite what is in that memory address, even write a new struct in there. So you're getting a "real" writable memory address. You could even write a different type of structs and break the program. You could tweak specific bytes.

In languages like Java or C# you aren't given a reference to the memory address but a reference to the object. You can only write to the object using it's own interface (methods) but you can't say "I'm going to totally overwrite this memory address with a new object".

If you receive an object in a parameter, let's say a "Person person" object and you do something like "person = new Person();" you didn't really overwrite the memory address. The original person reference that was passed in the parameter is still intact. You can only modify it with something like "person.setName(...)".

So, with real pointers you can do more stuff, but higher level languages don't want you to do that because it breaks some of their principles for what "good programming" is. In this case they are protecting encapsulation. You shouldn't be able to mess around with the memory contents of objects directly, only through their interfaces. Encapsulation is safer because objects should expose how to operate them safely via their interfaces.

[–] pazukaza@lemmy.ml 10 points 1 year ago

I guess this is beating a dead horse but you can have pointers to pointers for 2D arrays.

The first pointer tells you which coulm you're on. The second pointer tells you which is the first object of each column. That way you can iterate the columns without loosing a reference to the current column you're standing on.

[–] IHeartBadCode@kbin.social 9 points 1 year ago

char**

So that you can have an array of strings. It's useful to remember that in C arrays and pointers are exactly the same thing, just syntax sugar for however you want to look at it. There are a few exceptions where this isn't true however:

  1. Argument of the & operator
  2. Argument of sizeof
  3. C11 has alignof which decay is a no-no
  4. When it's a string literal of char[] or wide literal of wchar_t[], so like char str[] = "yo mama";

But int** is just an array of int*, which likewise int* can just be an array of int. In the picture here, we have int** anya that is an array of int* with a size of 1, int* anya that is an array of int with a size of 1, and then of course our int there being pointed to by int* anya.

[–] curioushom@lemmy.one 7 points 1 year ago

Quick example in straight C would be a cell in a matrix. The first pointer points to the row and the second pointer points to the cell in that row. This is am over simplification.

Why would it be redundant? You can’t even get past the main function before dealing with a char**

[–] h3ndrik@feddit.de 3 points 1 year ago* (last edited 1 year ago)

Not at all. In the picture above, the girl would be saying: "She knows where it is." This concept is used often in real life and in programming.

[–] ShroOmeric@lemmy.world 10 points 1 year ago

This is why I follow communiities like this: I can learn a lot by just reading the comments on a meme. Amazing.

[–] ramplay@lemmy.ca 6 points 1 year ago

Now someone augment it to show what happens when we dereference the pointer.

Even better derefencing a pointer that isn't pointing at anything

load more comments
view more: next ›