I always get confused with pointer to an array. So, I made some examples showing my confusion. I would appreciate if someone could answer those.
I have marked "main" code in part-1 to part-3 and I have question from those parts.
In part 1 & definition of function "func":
Q.1) function template is:int func(int* , int* ). How come it accepts func(B, pmult); in part-1 where first argument is an array rather than a pointer.
Q.2) Inside the function, Why can not I write: *B[4] = 5;
Or why does this works B[4] = 5;
Part 2:
Q.3) Just ensuring that I understand it correctly. I can not find the size of array if I have access to its pointer. In other words, I can not find the size of array B from inside function "func", Am I right?
Part 3:
Q.4) Why can not I write this int * p = &numbers; *p = 10; // This does not work
Or, why this works? int * p = numbers; *p = 10;
Q1) Arrays can be implicitely converted to pointers to its first element
Q2) B[4] is equivalent to *(B + 4): advance pointer 4 spaces forward and dereference it. Result of B[4] is lvalue of type int. As you cannot dereference int, you cannot do *B[4]
Q3) Yes, converting array to pointer will erase all information of its type and attempt to calculate size of array will come out terribly wrong. That is why this method of array size calculation is not recommended in C++ as unsafe.
Q4) Because array has its own type: int[5] in your case, so its address is of type int(*)[5], which cannot be converted to simple int pointer. (And even if it was allowed, you will have an address of array, so it would have to be at least double pointer). You have to rely on implicit conversion of array to pointer.