ARRAYS AS ARGUMENTS

As we seen before during the pass by value the change in arguments inside the function doesn't have the effect on real arguments in calling code. At the first glance the exception of this rule are arrays. Let's take a closer look at the next example:
#include "stdafx.h"
#include "iostream"
using namespace std;

void Clean(int field[], int lenght){
       while (lenght--)
             field[lenght] = 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
       int b[] = { 5, 10, 15 };
       Clean(b, 3);
       cout << b[0] << endl
              << b[1] << endl
              << b[2] << endl;
       system("pause");
       return 0;
}

At the first glace we can conclude that array is passed by value and after the function call Clean() the members of the array b[] stay unchanged. However the execution of the program causes the permanent delete of all members of the array. The reason is that the array isn't passed by value but the pointer is passes to its first member. So the function over that pointer handles the original and not with the copy of the array. We can declare the previous function in the following way: 
void Clean(int *Array, int length);
Although this behavior of array as argument brings certain inconsistency into programming language, its entirely practical nature. Let’s imagine that function needs as argument pass the array which have several thousand members. When during function call members of array initialize members of new local array in function the process of function call would take a long time, not only because of operation of joining specific members but because of time needed for allocating memory space for new array. Furthermore local variables inside functions are placed on stack, the special part of memory whose length is limited. The placement of long arrays on stack would very quickly fill this memory which would cause program to stop working.
During declaration it’s possible, but it’s not necessary to put the length of the array – the pointer is transferred to first member. For that reason the next three declarations of function Clean are the same:
void Clean(int field[], int length);
void Clean(int field[], int length);
void Clean(int field[], int length);
The length of the array is given as additional argument in function Clean(), which enables that function can be called for cleaning the array of arbitrary length.

Strings are also arrays so for them we apply the same rules. That means that function needs to transfer the pointer on first member. Strings are concluded with zero-sign, data of length array isn’t necessary to be transferred. For example: 
Share on Google Plus