C + + array is passed into the function int sum as a parameter_ arr(int arr[], int n)

First of all, we should know that C + + has restrictions on the type of return value -- it can't be an array.

Let's look at the statement int sum_ Is arr (int arr [], int n) correct?
This statement is correct, but doesn't it mean that the return value can't be an array? Why is int arr [] OK? Is int arr [] really an array?

arr is not actually an array, but a pointer.

We have learned array. We can know that the array name is regarded as a pointer. Our array name is the address of the first element of the array, so we use its address instead of the array.

Is there any exception? Actually, there are.
(1) Array declarations use array names to mark the location of storage.
(2) Using sizeof on the array name will get the length of the entire array in bytes.
(3) When the address operator & is used for an array name, the address of the entire array is returned.

So we can think:
int sum = sum_arr(cookies, ArSize);
int sum_arr(int *arr, int n);


These two statements are the same. We just replace int arr [] with int *arr. If and only if used in the function header or function prototype, int *arr and int arr [] have the same meaning. They both mean that arr is an int pointer.

Among them, we need to understand two important formulas:
arr[ i ] == *(arr + i)
&arr[ i ] == arr + I
Remember, adding 1 to the pointer (including the array name) actually adds a value equal to the length (in bytes) of the type pointed to by the pointer. It can be understood that the pointer is offset by i elements.

What does it mean to take an array as an argument?
int sum_arr(int arr[], int n) actually does not pass the contents of the array to the function, but the position (address), type (type) and number of elements (variables of n) of the array are submitted to the function.

When passing a regular constant, the function will use a copy of the variable; However, when passing an array, the function will use the same array as the original. But this does not violate the C + + method of passing by value, sum_arr () still passes a value, which is assigned to a new variable, but the value is an address, not the contents of the array.

Corresponding the array name to the pointer and taking the array address as a parameter can save the time and memory required to copy the whole array, but using the original data increases the risk of data destruction. We can use const qualifier to solve this problem.

Summary:

1. We can write the array into the function as a formal parameter, eg: int sum_arr(int arr[], int n). We should correctly understand that int arr [] is not an array, but a pointer. We just write it in the form of an array. Its essence is still a pointer.
2. To tell the array handler the array type and the number of elements, we need to pass them two different parameters, eg: void fillArray(int arr[], int size)

3. Don't try to pass the array length using square bracket notation. eg: void fillarray (int arr [size]) is wrong.

Program example:

#include <iostream>

const int ArSize = 8;

using namespace std;

int sum_arr(int arr[], int n);

int main(void)
{
	int cookies [ArSize] = {1,2,4,8,16,32,64,128};
	cout << cookies << " = array address, " << endl;
	cout << sizeof(cookies) << " = sizeof cookies " <<endl;
	
	int sum = sum_arr(cookies, ArSize);
	cout << "Total cookies eaten: " << sum << endl;
	sum = sum_arr(cookies, 3);
	cout << "First three eaters ate " << sum << " cookies " << endl;
	sum = sum_arr(cookies + 4, 4);
	cout << "Last four eaters ate " << sum << " cookies." << endl;
	
	return 0;
}

int sum_arr(int arr[], int n)
{
	int total = 0;
	cout << arr << " = arr, " << endl;
	cout << sizeof(arr) << " = sizeof arr" << endl;
	for (int i = 0;i < n ; i++)
		total = total + arr[i];
	return total;	
}

Operation results:

0x9ff8fc = array address,
32 = sizeof cookies
0x9ff8fc = arr,
4 = sizeof arr
Total cookies eaten: 255
0x9ff8fc = arr,
4 = sizeof arr
First three eaters ate 7 cookies
0x9ff90c = arr,
4 = sizeof arr
Last four eaters ate 240 cookies.

--------------------------------
Process exited after 0.3507 seconds with return value 0
 Please press any key to continue. . .

Keywords: C++

Added by IronCannibal on Mon, 17 Jan 2022 00:00:09 +0200