3 ways of modifying arrays??

Until now, I thought that there are 2 differnt ways by passing arrays into functions, so that you can modify them (http://www.cplusplus.com/forum/general/79388/)

1. Passing by value (int *a)
2. Passing by reference (int (&a)[N])
But now I discovered, that you can pass the array ordinarily, and it will have the same effect:
3. Passing ordinarily (int a[])

My question now is, if any of these 3 ways has special advantages or disadvantages.

Also, I do wonder if the same applies to matrices.
1. Passing by value (int *a) - passing a pointer (to the first element) by value

2. Passing by reference (int (&a)[N])

3. Passing ordinarily (int a[]) - this is not a third new way; it is the same as 1.
passing a pointer (to the first element) by value

In 1. (alias 3.) information about the size of the array is lost; if required it must be passed as a second parameter.

In 2. N must be a constant positive integer known at compile time.

> Also, I do wonder if the same applies to matrices.

There are no matrices, only arrays. What we may call a matrix is an array in which every element is an array.
Ok, thanks. I didn't understand what you mean with
 There are no matrices, only arrays. What we may call a matrix is an array in which every element is an array.

I also wonder, what you mean with
 to the first element
I'm sorry, but I still have problems with the expressions...
> Ok, thanks. I didn't understand what you mean with there are no matrices, only arrays.
> I also wonder, what you mean with to the first element

Where `T` is a type and `N` is a constant, when we define:
`T a[N] ;` we are defining an array of `N` elements of type `T`.

`T` can be any type. The array itself has a type: 'array of N objects of type T' and we can have arrays where the type of every element is an array.

An expression of type 'array od some kind' can be implicitly converted to a pointer; the result of the conversion is a pointer to the first element of the array.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#include #include #include int main() { int a[20] = { 1, 2, 3, 4 } ; // array of 20 int decltype(a) b = { 1, 2, 3, 4 } ; // b is of the same type as a - an array of 20 int typedef int array_type[20] ; // 'array_type' is an alias for 'array of 20 int' using array_type = int[20] ; // 'array_type' is an alias for 'array of 20 int' array_type c = { 1, 2, 3, 4 } ; // c is an array of 20 int // b and c are of the same type std::cout << std::boolalpha ; std::cout << "b and c are of the same type: " << std::is_same< decltype(b), decltype(c) >::value << '\n' ; // true array_type d[5] = { { 1, 2 }, {}, {6} } ; // array of 5 array_type int e[5][20] = { { 1, 2 }, {}, {6} } ; // array of 5 array of 20 int // d and e are of the same type std::cout << "d and e are of the same type: " << std::is_same< decltype(d), decltype(e) >::value << '\n' ; // true int f[5][15] = { { 1, 2 }, {}, {6} } ; // array of 5 array of 15 int // d and f are of different types std::cout << "d and f are of the same type: " << std::is_same< decltype(d), decltype(f) >::value << '\n' ; // false // an (expression of type) array can be implicitly converted to a pointer, // the result of the conversion is a pointer to the first element of the array. int* p1 = &(a[0]) ; // address of first element int* p2 = a ; // a is converted to a pointer, both p1 and p2 point to the first element std::cout << "p1 == p2 : " << ( p1 == p2 ) << '\n' ; // true }``````

http://ideone.com/0MiS2g
Topic archived. No new replies allowed.