Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of the address of a pointer?

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.

like image 828
walle Avatar asked Mar 12 '23 23:03

walle


2 Answers

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.

like image 108
mock_blatt Avatar answered Mar 24 '23 14:03

mock_blatt


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.

like image 36
ameyCU Avatar answered Mar 24 '23 13:03

ameyCU