4. Arrays as function parameters #
Created Wednesday 13 November 2019
Declaring and defining the variables in the same statement:
- data_type arr_name[] = { val1, val 2,…}; // creates an array of size = number of values n braces
- data_type arr_name[arr_size] = { val1, val 2,…val n}; // creates an array of size arr_size(assuming no. of values <= arr_size). Also the first n elements of the arrays are initialized to val1 val2 … val n. An the remaining are initialized to 0. Remember this.
- To create an array with all zeros just write, . data_type arr_name[arr_size] = {}
- When we pass the array as argument, the whole array should be copied due to *Call by Value, ***But, **this does not happen because only the head is copied, the who array is not copied.
Q) Reversing an array. Swap n/2(floored) times. On Both sides A) for(int i=0, j=n-1, a = 1; a<=n/2; a++, i++, j–) { int temp = arr[i]; ar[i] = arr[j]; arr[j] = arr[i]; }
- In C++, it is syntactically correct to exceed the valid range of indices for an array. This can create problems, since accessing out-of-range elements do not cause errors on compilation, but can cause errors on runtime. The reason for this being allowed will be seen in a later chapter when pointers are introduced.
- Library arrays using
http://www.cplusplus.com/doc/tutorial/arrays/