In this example actually the assignment will no work.
If value in your example is int
then it should be like this:
*count = value;
And the result is not ‘point to another variable’s value’, but assign value from ‘value’ to the address where count points’
One of the benefits is copy time. When you pass values of any type by value, that means copy them every time. For example:
int square(int nubmer) {
return number * number;
}
int a = 2;
int b = square(a);
When you call square(a)
function, and you pass a to it, the value of a
is going to be copied, so when you are inside of a function, you already working with a copy of a, that is represented as number
;
Same goes for return value:
When you return number * number
actually a copy of a result is returned and then assigned to b;
With small data types like int
or float
or char
this is not a problem, and copying is done fast.
But when you will start having custom data types created by you, or use those created by other developers, this can slow down the performance.
Another case is when you need pointers is when you will start creating your objects on a heap, and the only way of reaching them is to have a pointer to the area of memory where it was created. That is a bit more advanced topic. You will reach it later