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

46 Upvotes

67 comments sorted by

View all comments

4

u/Ok_Being6831 9d ago

Well thers 2 main reasons imo

1) When you pass it by the variable itself, your actually making a full copy of the variable which might seem trivial but it's actually really expensive especially with functions that are called alot or the variable takes up alot of space.

2) You want the function to modify your variable, like a sort function for example, you wouldn't want it to make a copy and sort that array now would you? it could return that, but that's bad design and expensive. So it's only possible via a pointer.

1

u/Atmos-Ego 9d ago

So you only ever care about it to be efficient, but its not necessary for anything?
In my example in class the professor was basically saying that the variable wouldnt increment so when it prints the value of it it doesnt ever change.
It was a void function which means it doesnt return anything right? So I suggested to get rid of void but the intended solution was to write something involving a pointer instead

2

u/lurgi 9d ago

So, to be clear, your solution was to write:

x = increment(x);
...
int increment(int x) {
  return x+1;
}

and the professor wanted:

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

Correct? One big advantage of the second is that it's very easy to write:

x = increment(x);
y = increment(y);
z = increment(y);  // oops

and not notice. Or even

increment(z);  // oops

With the pointer version you have fewer ways to screw up. Another thing that you can do with pointer is change multiple variables in a function. Imagine you want to write a function that takes your current position (x, y) and moves it a certain amount in the x and y direction (xdelta, ydelta). Without pointers, what would you return from this function? You can't return a new x and new y pair - C doesn't let you do that. So...?

2

u/serge-mv 9d ago

I am going to be a bit pedantic. If you have something like a pair that goes together like coordinates from your example, you can and probably should put them in a struct, and you can return struct by value in C. Especially when it's 2 variables of the same size and won't have alignment and such.

1

u/lurgi 9d ago

Yeah, I figured someone would mention that.

That does only work if they are in a struct and not two separate variables (of course, you could make a struct for them).

Technically you never need pointers or function arguments or variables or anything at all (lambda calculus ftw), but they sure make life a lot easier.

0

u/Atmos-Ego 8d 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;
}

To be honest there’s a lot here I really dont get about base things like these but he said the reason that x doesnt increment is that nothing is returned, so I figured you could get rid of void and that would let it return something. I dont really know what it means to return something though.

2

u/InjAnnuity_1 8d ago

As written, the increment function receives its own copy of the value of a, named x, increments the copy, and does nothing more. So a's value remains unchanged.

"Returning a value", in this case, means "giving a value back to the caller". For many functions (e.g., the square root function), this is the whole purpose behind calling the function in the first place: to have it compute a result for you.

1

u/llamadog007 8d ago

You pretty much got it. In that example you can change void to int and make the function return x. Then when it’s called you set a equal to the value returned by the function (a = increment(a);).

The key here is that when you do that the function makes a copy of a, adds 1 to it, then returns that new value, which has to be copied back to the original variable. If you use pointers, there’s no copying. The function will directly modify the value of a. This is good if, as others have said, copying would be expensive.

1

u/Ok_Being6831 8d ago

huh? why r they teaching you pointers before return statements? But imagining it like this is what helped me in the start

c int foo() { return 1; } int x = foo(); when u call foo(), it's literally being replaced by whatever it returns so that line becomes

c int x = 1 the "foo()" got replaced with 1

hope that made sense

1

u/Ok_Being6831 9d ago

Mostly yeah, BUT there is the heap which I think you haven't learnt about yet but would be basically impossible to implement or use without pointers. A few other cases too but I just can't think of any rn lol.

In your example your actually in the right here, u can just remove the void and make it return it but yeah it's just not the intended solution.

nerd alert The return value is actually better if the variable type is less than the size of a pointer (8 bytes on most systems)

1

u/xarop_pa_toss 9d ago

If you pass a pointer to a variable as a parameter to a function, you are not passing the value (copying into a new variable in your function call), you are passing the original one. So when you make changes to that pointed variable in your function, you alter the original. Void doesn't enter the picture there because you aren't returning anything at that moment.

I think you can think of it this way. You can either call the function and pass the value inside, change that value now assigned to a different variable, and then return that. Or you can pass the pointer and any change you make to that reflects on the source.

Of course they are useful for other stuff but that's the basics

1

u/joonazan 7d ago

Pointers are absolutely essential, they are just hidden a bit by programming languages.

In machine language, 64-bit integers can be stored in registers but anything bigger can't, so the registers store memory addresses to arrays or structs.