If we have code:
int b = 10;
int* a = &b;
std::cout << a << " " << &a << " ";
As the result, the addresses are different.
But what's the meaning of address of a pointer?
A pointer has the value of a variable's address, since we have a varaible in memory. But we don't have the value of address stored in memory, so why we have the address of an address?
Maybe I have some misunderstandings, thank you for your help.
Remember an address on your machine is going to be, itself, a 32 or 64-bit value (depending on your system architecture).
In your example, you have the integer b that stores the value 10 in some address, let's call it address 500
Then you have a pointer a, which stores the value 500, and IT has its own address.
What's the point? You can actually have double-pointers (or more).
You understand that in
char* string = "hello";
string is a pointer to the beginning of an array of characters
Then
char** strings;
is a pointer to a char*. That's how you could do an array of arrays, for example.
std::cout << a << " " << &a<<" ";
Yes ,both are different .
1. a
has been assigned address of b
, so it prints address of b
.
2. &a
prints address of pointer a
itself .
And a
and b
don't have same address.
It's similar(to understand) to this example -
int b=9;
If you print b
you get its value i.e 9
but if you print &b
you gets its address , and in no ways they will be same .
Same is the case with pointers.
A pointer has the value of a variable's address, since we have a variable in memory. But we don't have the value of address stored in memory, so why we have the address of an address?
We declare a variable (pointers , array , just int
, char
) these all are declared in program and are stored in memory . As these are stored in memory ,they have their unique address.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With