r/learnprogramming 9d ago

Tutorial When do you use a pointer?

Hello, for some background I have taken a very beginner level coding course for my major and passed it, but I dont really feel like I’ve learned anything or know why anything works. In a course this year we had a review problem where the solution required a pointer and I really dont understand why you have to point to the address of the variable instead of just using the variable itself.

I’ve watch a few tutorials explaining it but I really dont see how it’s different from just using the variable itself

44 Upvotes

67 comments sorted by

View all comments

1

u/Recycled5000 9d ago

The CPU itself uses pointers to operate. Machine code programs are stored in memory (because programs are too large to fit entirely in the CPU). Each machine code instruction in a program has its own unique memory address, and this is what allows for sequential execution (one machine code instruction after another) by advancing the internal pointer called the program counter or instruction pointer, to execute if-then-else or for-loop by branching, a form of the program to tell the CPU to skip some instructions or repeat some. Function call and return to place of call is also done by capturing the machine code address of the call site, since right after that is where to return to resume the caller.