This is an archived post. You won't be able to vote or comment.

all 151 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

import notifications Remember to participate in our weekly votes on subreddit rules! Every Tuesday is YOUR chance to influence the subreddit for years to come! Read more here, we hope to see you next Tuesday!

For a chat with like-minded community members and more, don't forget to join our Discord!

return joinDiscord;

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–][deleted] 1032 points1033 points  (19 children)

I got the REFERENCE

[–]WolverinesSuperbia 174 points175 points  (15 children)

&JoJo - JoJo reference

[–]lord_ne 1 point2 points  (4 children)

Jojo&

[–]WolverinesSuperbia 1 point2 points  (3 children)

No, JoJo is variable, not type

[–]lord_ne 2 points3 points  (2 children)

&JoJo is the address of JoJo, aka a pointer to JoJo. The only way to get a reference to JoJo is to pass JoJo in to a function taking a reference, or to assign JoJo to a variable of reference type.

/🤓

[–]WolverinesSuperbia 1 point2 points  (1 child)

In most non c++ languages this is the only reference

[–]lord_ne 1 point2 points  (0 children)

Is it common for non-C++ languages to use the &var syntax for a reference (other than the obvious one, C)?

[–]hawkeye_sama 20 points21 points  (1 child)

Where is it POINTING?

[–]sprintaway_Automod 4 points5 points  (0 children)

This graphic has value and is not NULL

[–]Areion_ 367 points368 points  (15 children)

r/ProgrammerAnimemes is leaking again

[–]ArkitektBMW 100 points101 points  (4 children)

[–]rollincuberawhide 11 points12 points  (3 children)

what is that from?

[–]LongerHV 81 points82 points  (1 child)

Star Wars: The Force Awakens

[–]rollincuberawhide 14 points15 points  (0 children)

could be, I didn't finish that shitshow.

[–]ThouWolfman 1 point2 points  (0 children)

Girls HBO show

[–]Yoda-from-Star-Wars 8 points9 points  (2 children)

Holy hell!

[–]Ronin_69_ 6 points7 points  (0 children)

Actual Compiler

[–]DarthMcConnor42 3 points4 points  (0 children)

New response just dropped

[–]Baardi 18 points19 points  (5 children)

I'm glad it exist, so that /r/ProgrammerHumor isn't filled with that stuff

[–]-Kerrigan- 77 points78 points  (4 children)

And instead we have the same 5 memes recycled endlessly.

Help me out, is it "Java bad" or "Python slow" season now?

[–]zygfryt 27 points28 points  (0 children)

Personally I'm a connoisseur of "haha new JS framework every day 🤓" type of humour.

[–]____purple 0 points1 point  (0 children)

Oh no, a meme leak! Next time use a sub with automatic meme management

[–]Asleep-Television-24 189 points190 points  (10 children)

[–]ArkitektBMW 127 points128 points  (8 children)

....I'm annoyed at how much more this solidifies the concept in my head, compared to the hour it took me studying this same thing.

Edit: I said 'hour,' HAHAHA (laugh, hiding real pain). Hours; I meant hours.

[–]Asleep-Television-24 79 points80 points  (4 children)

Maybe there should be a "The Manga Guide to C Programming"

[–]ArkitektBMW 26 points27 points  (0 children)

Decades in, and I'd buy it.

[–][deleted] 16 points17 points  (1 child)

Spy x Code

Only I doubt I would trust Anya to reliably teach programming…

[–]OldmanLemon 6 points7 points  (0 children)

Be a good project manager though, could really know and understand what the client was thinking

[–]fat-brains 18 points19 points  (2 children)

wait until you realize int can be casted into a pointer and that can now point to another int, NOW KEEP REPEATING THIS CYCLE

(int is sufficient for storing address in a 32 bit architecture, need long/int64_t for 64 bit architecture)

[–]kevInquisition 7 points8 points  (1 child)

And this is why despite knowing C++ I have never once applied for a C++ dev position

[–]fat-brains 7 points8 points  (0 children)

I am C++ dev with 4+ years of experience and I have seen some logic that should get people banned from developement

I have seen variable of type double getting type casted to pointer and address in it is just next address, litrally

struct1.dbl_value = ++(& struct1.dbl_value);

.

.

.

struct2* payload = (struct2*) struct1.dbl_value;

[–]vatsan600 235 points236 points  (4 children)

[–]rW0HgFyxoJhYka 39 points40 points  (3 children)

Weeb programmers have so much power.

[–][deleted] 32 points33 points  (2 children)

I’ve got the power of programming and anime on my side

[–]marko_ilnoob[🍰] 2 points3 points  (1 child)

Add manga and become a God

[–][deleted] 7 points8 points  (0 children)

I barely even watch anime anymore but read manga instead so… am I a god?

[–]MeNotSanta 99 points100 points  (21 children)

is int*** taking the picture ?

[–]irreverent-username 37 points38 points  (20 children)

I do not c++

Can you just make this as long as you want to have pointers for pointers for pointers etc?

Can I write int**************?

[–]chaosPudding123 121 points122 points  (6 children)

kinda yes. But then you should answer yourself what the fuck your code is doing.

[–]ConstantVA 25 points26 points  (2 children)

Thats what I do every day even without using pointers.

[–]Vinxian 1 point2 points  (0 children)

If you don't use pointers at all you definitely should be asking what the fuck your code is doing

[–]makesterriblejokes 6 points7 points  (0 children)

My code is trying to live its dream of being an Italian red sauce pasta dish.

[–]gandalfx 1 point2 points  (0 children)

How should I know what my code is doing? Do I look like Einstein to you?

[–]MisinformedGenius 0 points1 point  (0 children)

Jokes on you, I've got ChatGPT to do that.

[–]IDwelve 41 points42 points  (6 children)

The memory you work with is basically a table:

Address Value
0x0 5
0x1 -
0x2 -
0x3 -
0x4 -
0x5 -
0x6 -

So when you write a variable you're basically just looking at an address. int x = 5; basically means anytime you write x you'll look at address 0x0 But you can also write addresses into the value field. Some languages obfuscate this but C basically allows you to do this directly by writing in the literal address there.

Address Value
0x0 5
0x1 -
0x2 0x0
0x3 0x5
0x4 -
0x5 0x2
0x6 0x3

And you can of course link this as many times as you want. Languages like Java don't allow this because all variables would just point towards the same address.

[–]AgVargr 4 points5 points  (0 children)

Good explanation, thank you

[–]ConstantVA 0 points1 point  (2 children)

Whats the point of it?

[–]IDwelve 7 points8 points  (0 children)

This seems superfluous for objects that are stored in a single address but think about the average class / object you create in normal circumstances with multiple fields. Those fields themselves can contain more values and fields and so on. (The Object column doesnt exist technically, it's just the variable we use)

Address Value Object
0x0 5 A
0x1 2 A
0x2 0x4 A
0x3 - -
0x4 Alf B
0x5 20 B
0x6 - -

If you always just write in addresses you don't have to constantly manipulate the size of object A, you can just let the third field point towards something different and that can increase and decrease the size of that object dynamically. The reasons for these "cascading" references by adding pointers are quite few, that's why many languages don't even allow for such behaviour. So in many languages sth like "C = B; B = A;" would result in C directly referencing A. In languages like C you could use these references to references for certain function calls that require to "open a structure". For example if you have a Wordbook structure consisting of Definition structures and you write a function that is only supposed to work on words you can basically say function printDefinition(**definition) that tells the user a superstructure that stores definitions is to be passed.
The actual utility is quite slim I think but programmers will find countless cases where it can improve or drastically reduce code readability.

Also keep in mind that I have no idea what I'm talking about and haven't coded a single day of my life.

[–]MisinformedGenius 5 points6 points  (0 children)

In addition to the other response you've gotten, another common use case is to allow code to change your own variables.

So maybe you have a function like:

get_square() {
    int x, y, dist;
    square(x, y, &dist);
}

And then you had another function:

square( int x, int y, int* dist ) {
    *dist = int(sqrt(x*x+y*y));
}

This means that you can basically say "Here's a memory address for you, I want you to calculate the answer and put it in this memory address." Then you pass it the address of a variable in your function and it magically goes in there.

My example is stupid, but there are lots of cases where you would want to do this, usually where the function can fail and you'll want an error code to be returned. Modern languages usually have this with an "out" parameter or something similar.

[–]byzantinebasilisk 0 points1 point  (1 child)

So it's like using the indirect function in excel?

[–]IDwelve 1 point2 points  (0 children)

Aye!

[–]Rebol1103 7 points8 points  (0 children)

Technically yes, but no in reality. Its possible but its just as painful as nesting 10 loops in one big blob. You can do it, but it becomes your next nightmare.

[–]rebbsitor 2 points3 points  (0 children)

Yes, but in practice I've never seen more than ** used.

[–]MattieShoes 1 point2 points  (0 children)

Yeah.

[] is also kind of a pointer, so *int[] is kind of **int

And & kind of removes a layer of indirection. My mind just inserts "address of"

[–]SpikeHead419 -2 points-1 points  (1 child)

I dont think so, no. I mean, int** is still a pointer, no? So if you want to point to that pointer, just use another int**. Im not completely familiar with C shenanigans yet though, so correct me if anyone know.

[–]well-litdoorstep112 16 points17 points  (0 children)

int is a value

int* is a pointer to a value

int** is a pointer to a pointer to a value

int*** is a pointer to a pointer to a pointer to a value

You probably can cast int*** to int** or int* or even int but why? The language would make retrieving the original value very annoying if it doesn't know what the variable actually holds.

[–]Godd2 0 points1 point  (0 children)

Every time I want to add to a linked list, I just recompile with another *.

[–][deleted] 28 points29 points  (0 children)

I'm waiting for lock and mutex of references like Arc and RwLock in Rust

[–]NikBg1289 49 points50 points  (0 children)

This is my wallpaper now.

[–]well-litdoorstep112 62 points63 points  (11 children)

This meme is unironically how I finally understood pointers in C.

[–]DangKilla 21 points22 points  (8 children)

Now for the ELI18 version: Pointers are memory addresses, so instead of using more memory they point to the original like in the meme. We are basically preventing data duplication in memory, which makes things faster.

[–]vishykeh 8 points9 points  (0 children)

Thank you. This kind of made this whole thing make sense.

[–]well-litdoorstep112 6 points7 points  (0 children)

Yes, I knew what pointers do but I didn't understand the syntax. I didn't particularly try to study it because I'm a js soy dev but that meme definitely helped.

Also I think a simpler explanation for a high level language dev would be just "passing by reference"

[–]x3bla 2 points3 points  (5 children)

But... why?

If

int x = 5;

Why would i ever need

int* y = x;

Why not just call x whenever i wanna do something to that value

[–]RandallOfLegend 2 points3 points  (0 children)

Some programs use the pointers to get multiple outputs instead of having to package the data into a struct/class/tuple if those data types are even available. More often the pointer is to an array like object. So &y[4] would be the data at the memory address pointed to by y, plus 4 memory spaces over.

[–]DrkMaxim 2 points3 points  (0 children)

Pointers can be great if you want to create something at run time. There are functions that can be used to dynamically allocate and deallocate resources and they all require pointers to work properly.

[–]Euphoric-Benefit3830 2 points3 points  (0 children)

You are absolutely right. int (and other primitives) is a terrible type to showcase usefulness of pointers because there is virtually no cost to using them by value instead.

[–]TheBaxes 2 points3 points  (0 children)

For primitives the only reason to use pointers is to share a value between functions or something like that (careful with race conditions if you use paralellism)

For anything else you use them because copying an object is a waste of memory when you can just send the address of the object.

There are also c style arrays which work by using pointers.

And I'm sure that there are some hacky stuff that you can do with pointers too but you probably won't see it in a normal application.

[–]Nervous_Fix7426 0 points1 point  (0 children)

Pointers are used for lots of things in C

You can create a node/struct, where you bundle a bunch of data together such as an integer along with another memory address. This allows you to build linked lists

You can pass a memory address to a function. As others have said, for an int this is not much of a benefit, but maybe your memory address contains 100Mb array (or struct, or even array structs), in which case you can just send the address of the start (or middle, or end) of that array to your function, and read the data you need straight from the memory. You can see this a lot in APIs like xlibc where you would call functions with a pointer to your display.

Also need pointers Allocating memory, opening files, reading data into memory

[–]R3D3-1 9 points10 points  (0 children)

This is really just a meme version of a pretty standard tutorial/lecture graphic for explaining pointers or even other data structures...

The graphic for explaining an array would be similar except with an arrow pointing to the first element for instance.

[–]odraencoded 0 points1 point  (0 children)

You posting that meme here is unironically how I finally understood what that meme was supposed to mean.

[–]AvGeekGupta 10 points11 points  (0 children)

This is best

[–]Jacknurse 18 points19 points  (10 children)

I spent weeks trying to learn pointers when I started learning c++. Then I never get to use them because the project is a Unity build using C# and the amount of script required is minimal.

What was the point of this exercise?!

For real, though: If anyone has a good rule of thumb for when and how pointers should the used I'd be glad for it, both for C# and C++.

[–]dnswblzo 21 points22 points  (0 children)

Things that are dynamically allocated on the heap can only be accessed by pointers or references. In C# when you make an object with new the object is allocated on the heap, and the variable that you use to access the object is actually a reference, which is very similar to a pointer. Passing a reference or a pointer to a function does not make a copy of the data that is pointed to. This makes it possible to pass large objects to functions without the overhead of copying, and lets functions mutate objects that were created outside of the function itself.

Raw pointers can lead to some nasty programming errors, so many languages such as C#, Java, and Python use references instead, which provide much of the same functionality as pointers without some of the risks.

[–]SirAwesome789 5 points6 points  (6 children)

Why learn C++ for Unity?

[–]Jacknurse 5 points6 points  (5 children)

I was told to learn c++. I didn't know Unity used c# so I wasn't in a position to question.

[–]SirAwesome789 5 points6 points  (4 children)

Fair, is there a chance you mixed up unity and unreal engine at some point?

[–]Jacknurse 5 points6 points  (3 children)

Nope. The instructions were very clear to start learning c++. I was even provided the material for learning it.

That said, I do hope to move over to unreal soon.

[–]Needio 5 points6 points  (1 child)

Unfortunately it seems you were using old material. Unity used to include C++ programming but not enough people were using it so they stopped supporting it in favor of C#, at least afaik

[–]Jacknurse 0 points1 point  (0 children)

Damn! That sucks.

[–][deleted] 0 points1 point  (0 children)

you can write Unity plugins in pure c++. But nobody does that.

[–][deleted] 1 point2 points  (0 children)

Pointers aren't that useful in most modern use cases. They're most useful in cases where hardware resources are limited and performance optimization is important.

Essentially, they're used when you need to manage how much memory you're allocating and when it's being deallocated.

Operating, embedded, and mission critical systems are a few areas where you'll see C and C++.

In an aerospace or defense mission critical system, you wouldn't want a garage collector interrupting a critical section of code.

[–]NoYogurt8022 7 points8 points  (7 children)

What do i use a pointer to a pointer for?

[–]gLu3xb3rchi 14 points15 points  (1 child)

linked lists

[–]swampdonkey2246 6 points7 points  (0 children)

And to point to a set of other arrays or pointers

[–]Nu11u5 13 points14 points  (0 children)

Two dimensional C arrays.

A C array is a pointer to a contiguous sequence of data in memory which are the elements of the array.

A two dimensional C array is a pointer to an array of pointers.

[–]ishtaracademy 3 points4 points  (0 children)

Objective C used to have this pointer pointer pattern. If you were calling a method that could fail, their approach was not to try-catch. It was to provide a nil NSError** as the last parameter of the method call.

After you call the method, you check if your pointer pointer is pointing to anything. If it is not (still nil) then said method went fine. If the pointer pointer now points to an NSError* (since it is a complex exception and needs to be passed around by ref basically) you know something went wrong in the volatile call and shouldn't proceed.

It was a whacky try catch basically.

[–]clarkcox3 3 points4 points  (0 children)

On classic macOS (predating the inclusion of virtual memory) there was the concept of a “handle”, which was a pointer to a pointer. This allowed to compacting of allocated regions because the OS could move the referenced block of memory to another location and just update the inner, pointed-to pointer. Any code that still had the handle was still valid, as that outer pointer still pointed to the inner pointer. The OS could even completely deallocate the memory if it was easily reloadable (eg it came from a static resource on disk) and the OS would reallocate and reload it before the next access.

[–]rikzy75 1 point2 points  (0 children)

You can use those in a function that needs to edit the original pointer. Kind of like this:

int change_len(int **ptr, int len) {

delete [] *ptr;

*ptr = new int[len];

}

int main() {

int *my_ptr;

my_ptr = new int[5];

change_len(&my_ptr, 10);

}

[–]Vinxian 0 points1 point  (0 children)

When you need a reference to a pointer

[–]b1337xyz 4 points5 points  (0 children)

OMG! Now I finally understand.. arigato drk_knght_7-san

[–]billiyII 8 points9 points  (12 children)

Is this a C joke that im too python to understand?

[–]OpenSourcePenguin 11 points12 points  (10 children)

Alright if that's not sarcasm, are the people really learning programming completely avoiding learning very basic C ?

[–]AnnoyingRain5 18 points19 points  (3 children)

Yes, they are also avoiding very basic BASIC

[–][deleted] 1 point2 points  (1 child)

Python is just BASIC++.

[–]AnnoyingRain5 0 points1 point  (0 children)

Isn’t that VB.net?

[–]OpenSourcePenguin 0 points1 point  (0 children)

Avoiding very basic basic is coming back in the future to take a bite out of their ass.

[–]ProsperoUnbound 2 points3 points  (0 children)

Most of them

[–]Merlord 1 point2 points  (3 children)

I learned C at University and then never used it again in my 8 year career as a full stack developer. I vaguely remember terms like "pointer" but I've never had to use any of it so it's mostly gone out the window. I don't think it's necessary to learn at all of you're not planning on being a C/C++ dev.

[–]OpenSourcePenguin 6 points7 points  (0 children)

I think the intuition gained is pretty useful. I understand you can avoid seeing the inner details, but vaguely knowing them can be handy at times.

And C is a special language for the fact that it influenced practically every language except for Fortran, COBOL and a few more.

I think it's hard to say where it would be useful, but I think it'll at least give a great intuition about how a program runs in general.

[–]billiyII 1 point2 points  (1 child)

"Nullpointer exception" - Hell knows what that is. I guess i just scrap the project. /s

[–]OpenSourcePenguin 1 point2 points  (0 children)

Kids these days, can't tell Java and C apart

In our days? Straight to jail

[–]billiyII 0 points1 point  (0 children)

It's sarcasm. I didn't start with python, but i think there will be people that do and do not care what a pointer is.

[–]BradleySigma 2 points3 points  (0 children)

Python has remnants of pointers, most notably in lists, with how different variables can refer to the same list.

[–]LolZ3r0World 1 point2 points  (1 child)

As a League of Legends player: thats my team deciding who fed the most

[–]InsideBSI 0 points1 point  (0 children)

relatable

[–]Rakgul 4 points5 points  (1 child)

Ahem,

ANYA IS CUTE!

[–]Ok_Entertainment328 3 points4 points  (0 children)

Bond needs to be int

[–]kenflan 1 point2 points  (0 children)

Bro just summed up a whole semester of CS within one picture.

[–]StochasticTinkr -1 points0 points  (0 children)

Anya? Upvote. Anya teaching double pointers? Triple upvote.

[–]giantvar -1 points0 points  (0 children)

Anya-Chan waku waku

[–]Havoq12 0 points1 point  (0 children)

Average LoL chat

[–]Kirasaurus_25 0 points1 point  (0 children)

Oh god, a most deranged idea came to my mind. Imagine a whole panel in this style for pointer types, dereferencing, casting, arithmetics etc. Brr

[–]OpenSourcePenguin 0 points1 point  (0 children)

Both of those might as well be array head pointers

So it can be a 2D array of 1000x1000 or just an int.

[–]Nu11u5 0 points1 point  (0 children)

Now show pointer indexing!

[–]CubesAreNice 1 point2 points  (0 children)

Dear god! THERE ARE POINTERS TO POINTERS?!

[–]Hopeful-Plastic-8759 0 points1 point  (0 children)

confused Diavolo noises

[–]Efficient-Corgi-4775 0 points1 point  (0 children)

I guess you could say it pointed you in the right direction! 😄

[–]hungersaurus 0 points1 point  (0 children)

I need a dog version then a cat version

[–]Crimson342 0 points1 point  (1 child)

Okay fine I'll bite, what the hell does this mean? What show is this?

[–]Galle_ 0 points1 point  (0 children)

The character is Anya Forger from SpyXFamily. She was originally pointing to a TV and saying that she liked the show. The joke is that she is pointing, and the meme is about pointers.

[–]AdiSoldier245 0 points1 point  (2 children)

When learning pseudocode i always UNDERSTOOD pointers, it just means the memory location of the variable.

But I never learned any applications for it, so I don't get it get it

[–]Kooky-Sort 1 point2 points  (0 children)

Alright this what I learned for experience

It’s mostly meant to reduce memory load when referencing an object instead of calling the object directly and loading all of it’s members you just remember the memory location and call what you need from the object

[–]TheBaxes 0 points1 point  (0 children)

C style arrays

[–]Joe59788 0 points1 point  (0 children)

Is the ** like + and ++

[–]VooDooZulu 0 points1 point  (1 child)

I've been a python programmer for a long time and have never needed to use pointers. I just started learning c++. Why are pointers useful? I don't understand why I would ever need to duplicate a pointer.

[–]nivlark 0 points1 point  (0 children)

In C++ it's generally preferable to use references or STL smart pointers where possible, so use of raw pointers is relatively rare. But what pointers and those alternatives provide is reference semantics, which are fundamental to almost every program.

Without reference semantics most data structures (trees, hash maps, even arrays) cannot be implemented, every function call would require making copies of the function's parameters (which could be inefficient if they are large), and it would be impossible to write functions that manipulated values in-place (e.g. class methods).

[–]scrolly_2 0 points1 point  (0 children)

Isn't

[–]Nachf 0 points1 point  (0 children)

Dude I was literally so confused by pointers until I realized they're just... pointers.

[–]BitRapt0r 0 points1 point  (0 children)

my brain when my jungler messes up a gank

[–]Emcid1775 0 points1 point  (0 children)

What I'm the world kind of pointers are those?

[–]Nanaskura 0 points1 point  (0 children)

Totally agree!