59 points

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.

permalink
report
reply

pointer pointers

wait until you learn about template templates in C++

permalink
report
parent
reply
17 points

Are the templates in the room with us right now?

permalink
report
parent
reply
20 points
*

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

permalink
report
parent
reply
6 points

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

permalink
report
parent
reply
2 points
*

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.

permalink
report
parent
reply
58 points

That’s actually a pretty good representation.

permalink
report
reply
8 points

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

permalink
report
parent
reply
45 points

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

permalink
report
reply

here, have a peanut 🥜

permalink
report
parent
reply
18 points

I love peanuts!

permalink
report
parent
reply
25 points

int** int* interesting

permalink
report
reply
14 points

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

permalink
report
reply

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.

permalink
report
parent
reply
10 points

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

permalink
report
parent
reply
5 points

So it’s sort of like “proxying” through pointers to enforce memory isolation?

permalink
report
parent
reply

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.

permalink
report
parent
reply
9 points

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.

permalink
report
parent
reply
9 points

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.

permalink
report
parent
reply
8 points

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

permalink
report
parent
reply
7 points

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.

permalink
report
parent
reply
3 points
*

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.

permalink
report
parent
reply

Programmer Humor

!programmerhumor@lemmy.ml

Create post

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

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.

Community stats

  • 3.7K

    Monthly active users

  • 1.5K

    Posts

  • 35K

    Comments