我试图理解指针,但我无法理解一件事。 例如,
//part of code
int *p;
int tab[3];
现在我们有了包含数组第一个元素地址的变量tab
。我想创建一个指向该指针的指针。
所以,我会这样做:
p=&tab;
如果可行,p
将是 tab
的地址(这意味着它将指向 tab
)
不幸的是它不起作用。那么如何到达指针本身的地址呢?
地址的正确类型是什么? (addresstype *p
) 我想指向包含 tab
第一个元素地址的内存块,而不是 tab
本身。
最佳答案
so now we variable tab that contains address of first element of the array.
不,我们有一个变量tab
,是一个数组。
And I want to create a pointer that points on this pointer.
如果您想要一个指向数组第一个元素的指针,那很简单:数组可以隐式转换为该指针(这导致人们普遍错误地认为数组是指针)。
int * p = tab;
如果你想要一个指向数组的指针,而不是第一个元素,那么这是不同的类型:
int (*p)[3] = &tab;
但您通常不会想要那样的东西。
So how to get to address of pointer itself?
没有指针,因此无法获取该地址。如果你确实有一个指针,那么你可以获取它的地址,就像任何其他对象一样:
int * p = arr; // Pointer to first element of array
int ** pp = &p; // Pointer to pointer
I want to point of the memory block which contains address of first element of tab, not on tab itself.
这没有意义; tab
是包含其元素的内存块。
关于c++ - C++中指针的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22454757/