I was looking for pointer to an array of integers. I know that int * means pointer to integer type. On same lines, I thought int *[6] means pointer to array of integers. It looks like some online posts are saying int (*)[6] is the correct one? Can someone please tell me which is correct? Why should we put the pointer in parentheses(*)? Does it work without the parentheses?
thanks @andywestken for that wonderful explanation.
I tried your code online and figured only int (*ptr)[6] works. INIT 1 & INIT 2 did not work, both didn't compile. In display, DISP 1 displays address, else displays the values.
I did not understand the reasoning behind it though. Why is brace-enclosed initializer needed here?
I came across this question while working on hash map implementation. This is not for an assignment :)
Because the compiler has already taken the * following the int to deduce that you want a pointer-to-int type (int *), so ptr ends up being an array of this type (these pointers.)
The brackets stop the compiler from associating the * with the int, forcing it to associate it with the variable (array).
Basically, it's because that's the way the C++ compiler understands C++ syntax!
I fall into the "type-ist" camp (cf. syntax-ist) and write this as:
Wow @andywestken, thanks for that explanation. You added lot more clarity to the concept with that explanation. I think I am much more comfortable with this syntax now, thanks to you :)