|
Passing Arrays |
To pass an array , specify the name of the array without any brackets.
For a function to receive an array , the function's parameter list must specify that an array will be received.
In 2 dim array you must mention at least the column size of the array.
In General , the first subscript of a multiple-subscripted array is not required , but all subsequent subscripts are required.
All array elements are stored consecutively in memory regardless of the number of subscripts.
|
Pass |
Function Call |
Function |
Prototype |
What it passed ? |
|
array elements |
int g[5]; findMin(g[2],g[4]); |
findMin(int g1,int g2); |
findMin(int ,int ); |
copy of the passed value (pass by value) |
|
complete array |
average (g); // NO [] |
average (int g[]); OR average (int g[5]); average (int g);//WRONG |
average (int []); OR average (int g[]); |
actual array (pointer to the array , address of the first element in the array.)(pass by reference). |
|
2 dim array |
int t[3][4]; calc (t) ; |
calc (t[][4]); OR calc (t[3][4]); calc (t[][]); //WRONG |
calc (int t[][4]); OR calc (int t[3][4]); calc (t[][]) //WRONG |
|
Address |
|
The name of an array is the same as the address of the array's first element. |
|
|
#include <stdio.h> |
Output: array = 0012FF64 &array[0] = 0012FF64 &array = 0012FF64
|

Why when we pass a complete array , the actual array is being passed not a copy of it ?
Because in large arrays making duplicate copies of the array for each function call would :
Waste computer storage.
Consume execution time.
|
const |
The type qualifier const prevent modification of array values in a function.
When an array parameter is preceded by the const qualifier , the elements of the array become constant in the function body and any attempt to modify an element of the array in the function body results in compile time error.
|
#include <stdio.h> |