#include <stdio.h>
int main(void) {
int x = 5;
int y = 10;
int* px = &x;
int* py = &y;
printf("the adress of %d is %p.\n",x,px);
printf("the adress of %d is %p.\n",y,py);
*px = 8;//change the value at the adress of px
*py = 18;
//what happens if instead of *px = 8, just x = 8?
x = 99;
printf("the adress of %d is %p.\n",x,px);
printf("the adress of %d is %p.\n",y,py);
//so It did change, what's the point of using *px when changing the value?
return 0;
}
Hello, I started learning C in school, and I bump into a difficulty which could be extremely simple, but at this point I do not know about it. I am trying to change the value of x, and I used the method *px = 8 first to change it. but right after that I used x = 99, and it changed too, so I do not know what is the difference between them. I appreciate any feedback, thank you!
CodePudding user response:
Q: I used the method *px = 8 first to change it
A: The value of both "x" and "*px" changed to "8", correct?
Q: but right after that I used x = 99, and it changed too
A: Cool. So now both "x" and "*px" are 99. This is what you expected, correct?
Q: so I do not know what is the difference between them.
In this example, they're both equivalent. They both accomplish exactly the same thing.
Why use pointers at all? Because the variable "x" is tied to a specific memory location: you can't change it.
The pointer "px", on the other hand, can be changed at runtime to point to DIFFERENT memory locations, if you needed to.
Why is that useful? There are many different reasons. For example:
When you malloc() dynamic memory, the system gives you a pointer. You don't know or care where in memory it's located: you just use it.
Pointers are frequently needed to traverse the elements in a linked list
The ability to increment ( ) and decrement (--) pointers simplifies many algorithms
Etc. etc.
You might find these articles helpful:
CodePudding user response:
Other answers explained what is happening, but in your question I also read you ask 'why' someone would want to use references instead of direct accessing of a memory address.
There are many reasons why anything is the way it is in computer science, but a few I think most people wouldn't argue that are helpful for a beginning theoretical understanding:
A lot of Computer Science as a discipline is efficient/optimal management of resources, in many cases computer memory, but I/O and CPU make up two other resources we manage to make 'good' software. You may recall that a simple computer can be thought of as a CPU, RAM, and Storage, connected by a "bus".
Up until relatively recently, most software wasn't being run on a machine with lots and lots of RAM. It was necessary that memory be managed for performance purposes, and though we have access to higher memory machines today - memory must still be managed!
We manage that in more recently created languages with abstraction - or in this context, doing things for people when we know with a high degree of certainty what they are trying to do. We call languages where much of resource management is abstracted and simplified as "high-level" languages. You are "talking" to the machine with your code at a high level of control relative to machine code (you can think of that as the ones and zeros of binary).
TLDR; Memory is finite, and pointers generally take up less space than the data they point to, allowing for an efficiency in address space utilization. It also allows for many other neat things, like virtualized address spaces.