Basic array knowledge Review, basic array knowledge
Basic array knowledge Review
I. Two-dimensional array initialization (refer to this for one-dimensional array initialization)
(1) int a [2] [4] = {1, 2, 3, 4, 5, 6, 7, 8}; write all the corresponding values of all elements.
(2) int a [2] [4] = {1, 2, 3, 4}, {5, 6, 7, 8}; assign a value to the branch.
(3) int a [2] [4] = {1}, {3}; assign values to some elements.
At this time, array a is 1 0 0 0
3 0 0 0
The above briefly reviews how arrays are initialized in code. The following describes how arrays are called as function parameters.
Real Parameters: Can Be constants, variables, expressions, functions, etc. No matter what type of real parameters are, they must have a definite value during function calling, in order to pass these values to the form parameter. Therefore, values should be pre-assigned, input, and other methods to obtain a definite value for the real parameter.
Parameters: "Formal parameters" are used when defining the function name and function body. They are used to receive parameters passed when the function is called.
2. array as function parameter ()
(1)Array elements as function arguments: When an array element is used as a function real parameter, the real parameter value is passed to the form parameter, which is a 'value transfer 'method. The data transmission direction isPassing from real parameters to form parameters, one-way transmission.
# Include <iostream>
Using namespace std;
Int max (int x, int y)
{
Return (x <y? Y: x );
}
Int main ()
{
Int a [5] = {1, 2, 3, 4, 5 };
Int m = a [0];
For (int I = 0; I <5; I ++)
{
If (max (m, a [I])> m)
M = max (m, a [I]);
}
Cout <m <endl;
Return 0;
}
(2)Array name as function arguments: When array elements are used as real parameters, the values of the array elements are passed to the parameter variables. When array names are used as real parameters,The address of the first element of the array is passed to the parameter..
For example, in the following code, the average value of the ten elements in a [10] is obtained and output in the console. The array name is used as the real parameter.
Here, the first element address (& a [0]) of the real parameter array a [10] is passed to the form parameter array. Therefore, if the form parameter obtains the address of the first element of the real parameter array, the form parameter array has the same address and value as the real parameter array.
# Include <iostream>
Using namespace std;
// Calculate the mean Function
Float average (float a [10] [10])
{
Float sum = 0.0;
Float ave = 0.0;
For (int I = 0; I <10; I ++)
For (int j = 0; j <10; j ++)
Sum + = a [I] [j];
Ave = sum/100.0;
Return ave;
}
// Main Function
Int main ()
{
Float a [10] [10] = {22.2, 22.44, 55.55, 44.66 };
// Whether it is a one-dimensional array or a two-dimensional array, the real parameters are array names, representing & a [0] [0]
Float result = average ();
Cout <result <endl;
Return 0;
}
3. reference array elements through pointers
First, int a [10] = {0}; int * p; p = & a [0]; then * p = a [0]; remember this, other pointer reference arrays are the promotion of these simple knowledge. Just look at the C language books.
Next we will focus on referencing the multi-dimensional array element with pointers !!!
Let's take a look at the differences between the two sections of code in the pipeline !!!!
Int main ()
{
Int a [2] [2] = {1, 2, 3, 4 };
Int (* p) [2]; // here the pointer Variable P points to a one-dimensional array containing two integer Elements
P = a; // you can directly assign a value here to point p to the 0 rows of the Two-dimensional array.
Cout <* (p + 1) + 1) <endl; // output a [I] [j]
Return 0;
}
And
Int main ()
{
Int a [2] [2] = {1, 2, 3, 4 };
Int * p; // pointer variable stated here, pointing to an array element
P = a [0]; // The value must be assigned here to point to the second row of the Two-dimensional array.
Cout <* (p + 3) <endl; // The value plus one is equivalent to moving one digit behind
Return 0;
}
Summary: when using a one-dimensional array or two-dimensional array as the form parameter, the real parameter is the array name.
Pointer: When * p is used as the row parameter in a one-dimensional array, the real parameter is array.
When * p is used as the form parameter in a two-dimensional array, the real parameter is * array
When (* p) [3] is used as the form parameter in a two-dimensional array, the real parameter is array;