Reputation: 15
I'm a bit new to understanding pointers so I was hoping someone could check this to help me make sure I've got the right idea on how pointers work. I'm trying to simplify the idea of what a pointer is and how to use them
functA(int *numb){
functB(numb);
}
functB(int *numb){
functC(numb);
}
functC(int *numb){
*numb+=1;
}
int main (){
int testNumber = 0;
functA(&testNumber);
.
.
.
}
So in main, I'm sending the address of testNumber to funct A. So when I send this address to functA, there is no problem, correct?
Since funct A has a pointer parameter, which can hold an address, it won't run into a problem when I send an address, right? Then, since numb (in functA) is a pointer, I can send it to an functB because functB can accept an address. Also, I'm still sending the same address that testNumber is in, correct?
Again, I send numb (from functB) to functC and in functC, I'm derefencing this location (is there a better way to say this?) and increasing the count by 1. So when this is done, testNumber should be 1, correct?
I know this might sound silly and all, but I'm just trying to grasp this conceptually. I spent more time than I'm willing to admit trying to get this down concept on a project I just submitted (I did it, but it took way too long)
Upvotes: 0
Views: 1732
Reputation: 2259
A variable is stored at a location in memory and has a value stored in it. Every location has address.
variable
location_value
location_address
A pointer (say, int *p
) is simply a variable like other variables(say, int a;
). Difference is a pointer stores address of some location (which you already know).
variable: int testNumber = 10;
location_value: 10
location_address: 0xABCD000 // <-- 'testNumber' is stored at this location
----------------------------
variable: int *numb = &testNumber;
location_value: 0xABCD000 // <-- note: value of 'numb' is an address, address of 'testNumber'
location_address: 0x1234000 // <-- numb itself is stored at 0x12340000
Since, numb
is a variable you can go on assigning its value to other variable of same type.
In your case functC::numb = functB::numb = functA::numb = &testNumber = 0xABCD000
. which is correct.
So, when you dereference *functC::numb
you are accessing the variable which is stored at address functC::numb
, which happens to be testNumber
. So testNumber
gets incremented when you did *functC::numb += 1;
Upvotes: 0
Reputation: 47794
Way too many question in this post. As long as you're deferencing correct address, you'll be happy. But too much pointers here and there likely is going to cause some bugs, be careful about it.
So when I send this address to functA, there is no problem, correct?
Yes
Since funct A has a pointer parameter, which can hold an address, it won't run into a problem when I send an address, right?
Yes
Also, I'm still sending the same address that testNumber is in, correct?
Yes
.....is there a better way to say this?)
No and Yes, it depends on requirement.
So when this is done, testNumber should be 1, correct?
Yes
Upvotes: 2