简体   繁体   English

关于指针指针的困惑

[英]Confusion regarding pointers to pointers

I am a bit confused about pointers to pointers and wasn't able to find desired results due to Google omitting out some symbols. 由于Google省略了一些符号,我对指针的指针感到有点困惑,并且无法找到所需的结果。 What does the below statement actually mean 以下陈述实际上意味着什么

int** arr[10];

Is it an array of 10 double pointers or is it pointer to array of 10 integer pointers(or both the statements are same). 它是一个由10个双指针组成的数组,还是指向10个整数指针数组的指针(或两个语句都相同)。

What does the below statement depict? 以下陈述描述了什么?

*arr[0] = new int(5); //assign the first pointer to array of 10 pointers a memory of 5?

And is the first statement equivalent to 并且是第一个相当于的陈述

int* (*arr)[10];

It would be appreciated if someone clears my doubts. 如果有人清除我的疑虑,将不胜感激。 Thanks! 谢谢!

What does the below statement actually mean 以下陈述实际上意味着什么

It means an array of pointer to pointer to int . 它表示指向int的指针数组。 There are exactly 10 elements in the array, and each element has a type of int** . 数组中只有10个元素,每个元素都有一个int**类型。

What does the below statement depict? 以下陈述描述了什么?

It first accesses the first element in the array and then dereferences it to access the int* . 它首先访问数组中的第一个元素,然后取消引用它以访问int* The int pointer pointed to by the first element is assigned to point at the newly allocated memory. 第一个元素指向的int指针被指定为指向新分配的内存。 The newly allocated memory holds a value of 5 . 新分配的内存值为5

And is the first statement equivalent to int* (*arr)[10]; 并且是第一个等同于int *(* arr)[10]的语句;

No, not at all. 一点都不。 int* (*arr)[10]; is a pointer to an array of int* . 是一个指向int*数组的指针。 In other words it isn't an array, it is a pointer to an array, and the array that it points to contains 10 elements which are all int* . 换句话说,它不是一个数组,它是一个指向数组的指针,它指向的数组包含10个元素,它们都是int*

The [] notation has precedence over * when you make declarations. 进行声明时, []表示法优先于* So something like 所以像

int* arr[10];

is actually an array of 10 pointers to integers even though read from left to right it naturally looks like a pointer to an array. 实际上是一个10个整数指针的数组,即使从左到右读取它自然看起来像一个指向数组的指针。

However if you adjust the precedence like so: 但是,如果您像这样调整优先级:

int (*arr)[10];

You get a pointer to an array of ten integers. 你得到一个指向十个整数数组的指针。

In your case int** arr[10]; 在你的情况下int** arr[10]; is an array of 10 pointers to pointers. 是指向指针的10个指针数组。 And it is not equivalent to int* (*arr)[10]; 它不等于int* (*arr)[10]; as the latter is a pointer to an array of 10 integer pointers (essentially in the former case you have an array whereas in the latter a pointer, a very important difference). 因为后者是一个指向10个整数指针数组的指针(基本上在前一种情况下你有一个数组,而在后者中有一个指针,这是一个非常重要的区别)。

Maybe the C++ experts could chime in on your question regarding new . 也许C ++专家可以就你的问题提出new的问题。

35 <---- I am a pointer to an int


35 <---- I am pointer to an int <---- I am a pointer to an int pointer

This code: 这段代码:

int** arr[10];

declares an array of 10 of those pointers on the far right up there. 在最右边的那个地方声明了一个由10个指针组成的数组。

int** arr[10];

int num = 35;
int* pint = &num;
int** ppint = &pint;
arr[0] = ppint;

int** ppint2 = new int*[5];
int num2 = 35;
int num3 = 45;
*ppint2 = &num2;
*(ppint2 + 1) = &num3;
cout << **ppint2 << " " << **(ppint2 + 1) << endl;

--output:--
35 45


int* pint2 = new int[3];
int* pint3 = new int[2];

*pint2 = 10;
*(pint2 + 1) = 20;
*(pint2 + 2) = 30;

*pint3 = 100;
*(pint3 + 1) = 200;

arr[0] = &pint2;
arr[1] = &pint3;

cout << **arr[0] << endl;
cout << *(*arr[0] + 1) << endl;

--output:--
10
20

A pointer is a variable that stores a memory address. pointer是存储内存地址的变量。 So let's say you have the int 10 stored at the address 1A. 因此,假设您将int 10存储在地址1A中。 You can create a pointer variable to 10, and the pointer variable's value will be 1A. 您可以将指针变量创建为10,指针变量的值将为1A。 Of course the pointer's value has to be stored in memory somewhere, and therefore you can create another pointer that stores the address of where the first pointer's value is located in memory. 当然,指针的值必须存储在某处的内存中,因此您可以创建另一个指针来存储第一个指针值在内存中的位置。 You do that by assigning the address of the first pointer to the second pointer. 您可以通过将第一个指针的地址分配给第二个指针来实现。 The first pointer's type is int*, and the second pointer's type is int**, ie the second pointer is a pointer that stores the address of an int pointer. 第一个指针的类型是int *,第二个指针的类型是int **,即第二个指针是存储int指针地址的指针。

*arr[0] = new int(5);

arr[0] is a pointer that stores the address of where some other pointer is located arr [0]是一个指针,用于存储其他指针所在的地址
*arr[0] says, "Get the other pointer, please" * arr [0]说,“请另取指针”
*arr[0] = new ... assigns the address returned by new to the other pointer. * arr [0] = new ...将new返回的地址分配给另一个指针。

OK, let's take this one question at a time. 好吧,让我们一次提出这个问题。

int** arr[10]; is an array of 10 pointers that point to integer pointers. 是一个由10个指针组成的数组,指向整数指针。 Declarations in C++ can generally be read more easily right-to-left. 通常可以从右到左更容易地阅读C ++中的声明。 Note that arr is an array, not a pointer -- though it can "decay" into a pointer to its elements (which would be of type int*** ). 请注意, arr是一个数组, 而不是指针 - 尽管它可以“衰减”成指向其元素的指针(其类型为int*** )。

*arr[0] = new int(5); involves a few steps. 涉及几个步骤。 First, an integer is allocated on the heap (that's what new does). 首先,在堆上分配一个整数(这就是new )。 It is initialized with the value 5 (the (5) bit calls the "constructor" of int ). 它用值5初始化( (5)位调用int的“构造函数”)。 new returns a pointer ( int* ) to the location of this newly allocated int in memory. new将指针( int* )返回到内存中新分配的int的位置。 This pointer is assigned to the pointer being pointed to by the first pointer-to-pointer in arr -- the * dereferences the first element for the assignment. 该指针被分配给arr中第一个指向指针所指向的指针 - *取消引用赋值的第一个元素。

int* (*arr)[10]; is somewhat more exotic, and is not equivalent; 有点异国情调,并不等同; it seems to declare a pointer to an array of 10 int pointers ( source ). 它似乎声明了一个指向10个int指针数组的指针( 源代码 )。

A good trick to decipher statements like this is to take it step by step and build up 解读这样的语句的一个好方法就是一步一步地构建它

this is an array of doubles 这是一系列双打

double array[5];

array is also a pointer so this is the same (however the first one the size is known to be five in the second it could point to any number) array也是一个指针,所以这是相同的(但是第一个大小已知为第二个,它可以指向任何数字)

double *array;

` `

now lets try something more general 现在让我们尝试更通用的东西

T *a; is a pointer to one or more Ts so is T a[x]; 是指向一个或多个Ts的指针,因此是T a[x]; where x is a number 其中x是数字

building up T **a; 建立T **a; here a is a pointer to one or more pointers that point to one or more Ts 这里a是指向指向一个或多个Ts的一个或多个指针的指针

that means that *a is a pointer to one or more Ts and something like 这意味着*a是指向一个或多个Ts的指针

*a = new T[5] is valid however a = new T[5] is not a correct one would be a= new T*[5] that creates 5 pointers to Ts *a = new T[5]有效但是a = new T[5]不正确将是a= new T*[5]创建5个指向Ts的指针

So back to your original examples int ** arr[10]; 所以回到原来的例子int ** arr[10]; here arr points to a static array of 10 int ** that means arr[0] = new int*[5] is a valid statement the following is valid C++ 这里arr指向一个10 int **的静态数组,这意味着arr[0] = new int*[5]是一个有效的语句,以下是有效的C ++

    int ** arr[10];

arr[0] = new int*[5];
arr[0][1] = new int[5];
arr[0][1][0] = 4;

std::cout << arr[0][1][0] << std::endl; // prints 4

delete [] arr[0][1];
delete [] arr[0];
    //note that arr doesn't need to be deleted as it wasn't dynamically allocated

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM