+ 1

POINTER

explain pointers with an example I don’t get it

25th May 2024, 8:56 PM
CLASSICBOI
CLASSICBOI - avatar
1 Réponse
+ 8
A pointer is simply said, just an integer. But other than a other than a normal integer a pointer does not store normal integers. It stores the address of a variable. Think about like a person. Person A lives in a house. Person B knows where the house stands and can tell others about it's location. But because person be knows where the house stands, he can go inside (dereferencing the pointer) and see the person inside, which is Person A. int varA = 4; int *pA = &a; pA know knows where varA lives, while varA only knows what value it has. This knowledge is useful, when you want to edit a variable in a function. Normally, when you pass a variable to a function it can only the the function it's value. If the function edits the variable, then the function variable changes, but not the source of the value. void foo(int x) { x = 5; } int x = 4; foo(x); printf("%d", x) // still 4 although the function changed it. That's because the function create a new copy of the variable.
25th May 2024, 9:28 PM
Jake Hempel