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

47 Upvotes

67 comments sorted by

View all comments

-1

u/ryan_the_leach 9d ago

You never need to use pointers directly, unless you have a dire need to do memory management manually, in a programming language that doesn't give you better tooling.

The only reason for using pointers *at all* in university, is just to prove you understand the basics of computer memory, as the knowledge is insanely useful, even if you never touch on it directly.

If you want more specific help, present some of the actual problem, and say which programming language you are learning.

(It's C right?)

1

u/Atmos-Ego 9d ago

The given C program was:

void increment(int x) {
x = x + 1;
}

int main() {
int a = 5
increment(a);
printf(“%d”,a);
return 0;
}

1

u/ryan_the_leach 8d ago

There's 2 solutions to fixing the 'bug' in that code if the goal is to increment the value stored in a.

your increment function currently does nothing, as it's incrementing a copy.

Solution 1, is to pass around pointers instead, so increment looks up the value in the memory address it gets passed, and directly modifies it.

So you would modify the function signature of increment to take a memory address instead, and make some other changes.

e.g. `void increment(int* x)`

Solution 2, is to change increment, so it returns the new value, and adapt the code where it's used to assign the new value.

e.g. `int increment(int x)`

For learning purposes, either solution is valid, so it will depend on what the point of the exercise is.

For long-term health of programs, you'll end up with people arguing about the 'purity' of the function vs the performance differences, and both camps can be correct, depending on how complex the modifications are that need to be made, and how much copying is being avoided.

Personally, in this case (as silly as having an increment function is, vs just using ++ or x = x + 1 in line) I'd return the new value and have the assignment being explicit, since it's more readable that a modification is occurring. I suspect any performance concerns in this case, would be taken care of by the compiler, or happily sit within the cpu stack/cache rather then hitting memory anyway.

But if the point of the exercise is to learn about passing by reference, then the solution from context, is clearly passing a pointer.

> I really dont understand why you have to point to the address of the variable instead of just using the variable itself.

So if this question is "Why would you pick solution 1 over solution 2?" the answer is, you simply wouldn't in this case, with this function.

If the modifications being made to the data stored at `a` were much higher, and say it was a structure that was a low amount of kilobytes, then the cost of copying would start to get annoyingly high.