What is the meaning of the address of a pointer?

匆匆过客 提交于 2019-12-01 12:49:58

问题


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.


回答1:


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.




回答2:


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.




回答3:


A pointer has the value of a variable's address

I assume by a variable you mean the object corresponding to the variable. Nonetheless, this is not strictly true, and it's the edgecases where we start to make distinctions between addresses and pointer values.

There are pointer values that don't point at objects at all. Consider null pointers, for example.

Addresses, by definition, do point at objects... or functions.

what's the meaning of address of a pointer?

The term address of a pointer makes sense if you can imagine a variable declared to store a pointer (e.g. T *var;) and you take the address of it (&var).

so why we have the address of an address?

An address is a type of pointer value, but pointer values don't point at values; they point at objects or functions. &a is different to a because the two pointers point at different objects; one points at a and the other points at b.




回答4:


Thanks for you all I knew what's wrong with it.

In fact I created a pointer in memory by int* a = &b; but I thought I didn't so it's my mistake.

But what I thought that you cannot output the address of something that does not exist in memory like cout<<&(&a),the compiler will tell you that "&" operator need a l-value variable.

And if you want to output the address of pointer to a, you define a pointer to pointer variable int **p2p=&a, and then you can cout<<&p2p , it works.



来源:https://stackoverflow.com/questions/35954132/what-is-the-meaning-of-the-address-of-a-pointer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!