-1
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 ?

πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175
  • Don't use raw pointers please! Also _half an hour_ of research doesn't actually allow you asking a question at Stack Overflow. – πάντα ῥεῖ Oct 08 '16 at 03:54
  • 1
    It's an array of `int*`. – songyuanyao Oct 08 '16 at 03:55
  • whats wrong with using raw pointers? –  Oct 08 '16 at 03:57
  • 1
    @PranjalMisra _"whats wrong with using raw pointers?"_ They are error prone and have loads of pitfalls. Use [smart pointers](http://en.cppreference.com/w/cpp/memory) or [containers](http://en.cppreference.com/w/cpp/container) instead. – πάντα ῥεῖ Oct 08 '16 at 03:57
  • @πάντα ῥεῖ thanks for the tip . –  Oct 08 '16 at 03:59
  • @PranjalMisra If someone trying to teach you something else, kick them ass as hard you can. – πάντα ῥεῖ Oct 08 '16 at 04:00
  • 2
    The moment every CS student learns `operator new`, and `operator delete`, they should watch [Don't use f*cking pointers](http://klmr.me/slides/modern-cpp/#1). Do it for a clas presentation. Seriously =P – WhozCraig Oct 08 '16 at 04:03
  • @πάντα ῥεῖ I was attempting a question in hackerrank(https://www.hackerrank.com/challenges/variable-sized-arrays) and there the use of containers was not allowed so the only way of solving the quesiton was with pointers. –  Oct 08 '16 at 04:04
  • @PranjalMisra - What do you mean that the use of containers is not allowed? I know that online coding sites are kinda funny, but hackerrank does not restrict you from using standard C++ and STL. – PaulMcKenzie Oct 08 '16 at 04:06
  • @PranjalMisra Don't waste your time at online code judge engines. – πάντα ῥεῖ Oct 08 '16 at 04:06
  • I suppose STL is garbage then. Because every implementation of `std::vector` I saw uses pointers... – lapk Oct 08 '16 at 04:19
  • @lapk It makes a difference using these pointers consciously but just merely for no need. – πάντα ῥεῖ Oct 08 '16 at 04:20
  • 1
    @πάνταῥεῖ And programmers cannot use pointers consciously? Telling people not to "ever, ever, ever" use pointers leads to questions like that. People should understand pointers first, not be afraid to use them and then go learn containers... – lapk Oct 08 '16 at 04:24
  • 1
    @lapk _"People should understand pointers first"_ No, that's advanced stuff. You should deal with the basics of the language has got at hand first. It's a big mistake in most of the c++ programming classes, see [here](http://dev-jungle.blogspot.de/2015/02/i-have-dream-im-dreaming-of-so-called-c.html) please. – πάντα ῥεῖ Oct 08 '16 at 04:28
  • @lapk *People should understand pointers first, not be afraid to use them and then go learn containers* -- And after a couple of weeks, those same students have quit C++ and picking up Java. The "learn containers" part never got off the ground. – PaulMcKenzie Oct 08 '16 at 04:41
  • @PaulMcKenzie _"The "learn containers" part never got off the ground."_ That's a silly argument. – πάντα ῥεῖ Oct 08 '16 at 04:45
  • @πάνταῥεῖ - How many C++ courses actually get to part about "learning containers" when most of the time is spent on pointers? Hardly any that I'm familiar with. – PaulMcKenzie Oct 08 '16 at 04:47
  • @PaulMcKenzie That's may be why I'm dreaming :P Industrry needs really differs from what's taught at school mostly atm. We always have to bang these people in shape, to get them productive and useful. I'm just bored of that. – πάντα ῥεῖ Oct 08 '16 at 04:55

2 Answers2

0

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.

πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175
-1

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 e.g. 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.

Syntac
  • 1,448
  • 8
  • 8
  • _"A array is not an object but a construct in memory the computer doesn't know about at runtime."_ Huh?? – πάντα ῥεῖ Oct 08 '16 at 04:17
  • @πάνταῥεῖ Hey, why are you writing this. Even if you not know about, it is true, and no reason to make fun of it. I agree, the std-lib approach is a nicer one, but he asked for an explanation – Syntac Oct 08 '16 at 04:34
  • _"A array is not an object but a construct in memory the computer doesn't know about at runtime."_ At least this needs serious explanation. – πάντα ῥεῖ Oct 08 '16 at 04:37
  • _"no reason to make fun of it."_ We hate fun here actually. – πάντα ῥεῖ Oct 08 '16 at 04:39
  • @πάνταῥεῖ Once compiled, the computer can't distiguish between an array and a pointer. You theoretically could use the [ ]-operator on pointers, but practically most compilers will refuse to compile it. It's not about arrays anymore, but about allocated memory and pointers on that memory. The fact it still works is only guaranteed through the compiler. If you had an array int a [10] you could access the next place in memory by using *(a+10). – Syntac Oct 08 '16 at 04:41
  • So you meant to explain [What is array decaying?](http://stackoverflow.com/questions/1461432/what-is-array-decaying) again? – πάντα ῥεῖ Oct 08 '16 at 04:44