简体   繁体   中英

Double pointer 2-d array declaration int** seq=new int* [n];

int n,q;
cin>>n>>q;
int** seq=new int* [n];          // what is the meaning of this ?

I have been trying to understand this array declaration for half an hour but the int* [n] is really confusing me a lot. Is there a more convenient way to write this declaration ? How can I have such type of declaration in cpp ?

Is there a more convenient way to write this declaration ?

The idiomatic way to declare that pointer array in c++ would be

int n,q;
cin>>n>>q;
std::vector<int*> seq(n);  

Though it's questionable why int* pointers need to be stored in the array.

A int* is a pointer (I assume you know those).
So int*[n] is an array of length n containing pointers on integers.
What confuses people most, is that variables containing arrays actually are pointers in C++. Because of that, the variable's type, we store int*[n] to, has to be int**.
The pointer points at the first element of the array, and eg seq[2] would be compiled into *(seq+2). A array is not an object but a construct in memory the computer doesn't know about at runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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