r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

5

u/SlyHolmes Apr 05 '22

Pointers made me quit C++ and programming. Any chance anyone has any good resources/videos explaining them?

1

u/__Fred Apr 06 '22 edited Apr 06 '22

You can learn other languages than C++ that don't need you to understand pointers, like Python. Java or JavaScript look similar to C++ and you also don't need to understand pointers. (You kind of do with Java.)


I think of pointers in c++ like indexes to a giant array. A pointer is a variable that holds a memory location.

&foo = index of foo in the giant array of all memory

*44859375 = data at the index 44859375 of the giant array

*(&foo) == foo

Normally, you would of course use a variable and not a literal number.

All the tricks you can do with array-indexes (or "indicees"), you can also do with pointers. For example you have an array with huge datatypes and you want to return a search result. You can just return the index pointing at the result instead, if you don't want to copy the whole structure/object. Similarly you can return the memory address of an object.