+ 1
& and *
Can someone help me understand pointers and address, and when is it best to use them? I have some university assignments, and in some parts, I need to utilize it to solve the code. I can’t wrap my head around it. It would really help a lot! ╰(*´︶`*)╯♡
3 Antworten
+ 3
RAMさん, you can also try this to help you understand:
Think of a variable as a house where your friend(the value)lives. The house has an address(the memory location)in a city(the computer's memory).
Now, using & is like asking, “What is the address of this house?"or"WHERE does my variable(friend)live(is stored)?" &var gives you their address.
Now suppose you jot down the address on a piece of paper, that paper is your pointer. It 'points at' where to go but doesn't give you your friend (value). Using * is like going to the address written on the paper(pointer) and opening the door to check in or play with your friend(the value).
I am not very experienced with C myself but I hope it helps you.
Feel free to let me know if you have further questions.
+ 3
When a variable is created in C, a memory address is assigned to it which is the location in the computer's memory where the variable's value is stored. Now, to access the memory address of a variable, you use the address-of operator (&). When placed before variable name(e.g.: &var), & returns LOCATION in memory where that variable is stored.
Note that memory address usually keeps on changing every time you run the code but remains fixed within a single run iff the variable isn't destroyed (local variables get destroyed when the function ends, static vars when the prg ends, dynamic vars when 'free' called).
Use & whenever you need to work with the location of a variable in memory,not its value eg:interracting with pointers,modifying variables,sharing memory across scopes(you can’t access local variables from outside their scope)etc. It's useful when you're manipulating data structures or arrays:when you want to pass large data structures efficiently,pass their address using & instead of copying all the data.
+ 3
* is used to declare and work with pointers(special variables that store memory addresses)When used in a declaration (eg: int *p; for an integer value or variable p)it should match the type of data it will point to. Note that it will hold the ADDRESS of an integer variable,not the value itself. But, when * is used in an expression, it acts as the dereference operator.Dereferencing a pointer means accessing/modifying the value stored at the memory address the pointer is pointing to. E.g., if p points to x, then *p gives the value of x, and *p = 20; updates x to 20 through its address. Thus, you can use pointers to indirectly access and change values in memory(can't be done directly as values are passed by copy in C)Use* to modify a variable from another function,access data through dynamic memory(like from malloc)work with nodes and elements of data structures eg:arrays,trees,linked lists that depend on memory addresses.
If a pointer isn't initialized or is set to NULL,dereferencing it will crash your program