Difference between TYPE* and TYPE[]

Dear all,

Could you please tell me what is the difference between the following array declaration patterns:

name TYPE*

vs

name TYPE[]

Moreover, if I pass my array to a function and I want that function to modify this array, wouldn't it be enough to pass the array pointer (be it name TYPE * or the array's name)? Or do I need to pass the array as a reference? (Such as in TYPE& name)?

And what goes on under the hood in these two cases? I know the memory for this array will be allocated in the stack, but I would like to know with a little more detail what goes on when an array is declared when using the aforementioned patterns.

Cheers
Little
name TYPE* is not legal C/C++. What you certainly mean if TYPE*name

Moreover, if I pass my array to a function and I want that function to modify this array, wouldn't it be enough to pass the array pointer (be it name TYPE * or the array's name)?
yes, it is and it's usually done this way. Additionally allways pass the size of the array

Passing an array as a reference looks like so:
1
2
3
4
void fun(ArrayType (&a)[5])
{
  ...
}


the alternative with pointer:
1
2
3
4
void fun(ArrayType *a, int size)
{
  ...
}


Note that the latter works only for 1 dimensional arrays. Multidimensional arrays are more complicated.

And what goes on under the hood
Nothing particular happens when you pass an array. In all cases you'll have a pointer to the first element of the array
Topic archived. No new replies allowed.