简体   繁体   English

指针数组和指向指针数组的指针

[英]Array of pointers and pointer to an array of pointers

I don't quite understand where the error is here:我不太明白错误在哪里:

int *parr[22];  // Array of int* pointers
parr[0] = ptr1;
parr[1] = ptr2;
//... 

int *(*pparr)[22]; // A pointer to a int* array[22]
pparr = parr; // ERROR

the error tells me error C2440: '=' : cannot convert from 'int *[22]' to 'int *(*)[22]'该错误告诉我error C2440: '=' : cannot convert from 'int *[22]' to 'int *(*)[22]'

how come that the types are not equal?为什么类型不相等? The name of the array should be equal to a reference to the first element of the array, something like数组的名称应该等于对数组第一个元素的引用,例如

parr => &parr[0]

so the line seems right to me所以这条线对我来说似乎是正确的

int*[22]可以衰减为int** ,但不能将int**分配给int*(*)[22]

As pparr is A pointer to a int* array[22] so you need to write由于pparrA pointer to a int* array[22]因此您需要编写

pparr = &parr;

You need to store address in the pointer and not the pointer itself.您需要将地址存储在指针中,而不是指针本身。

It is same like when you have就像你有一样

int a=3;
int *b;
b=&a;

You are storing address of a in b, similarly you need to store address of parr in pparr您将 a 的地址存储在 b 中,同样您需要将parr地址存储在pparr

EDIT: To clarify OP's comment编辑:澄清 OP 的评论

You can't assign the address of the first element, but the address of the pointer that is pointing to first element.(therefore pparr = &parr; )您不能分配第一个元素的地址,而是分配指向第一个元素的指针的地址。(因此pparr = &parr;

int *(*pparr)[22];  //This one is an array of function-pointers returning an int pointer. 

int **pptr;  //Points to an array of pointer

So you can write所以你可以写

pptr = parr;

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

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