Talking about arrays in C++ as function parameters

1. Thoughts triggered by an example

  Run the following code, what is the output? The example comes from "Sword Pointing Offer", I have modified the example in the book to make it work.

#include<iostream>
using namespace std;

int GetSize(int data[]) {
    return sizeof(data);
}
int main() {
    int data1[] = {1,2,3,4,5};
    int size1 = sizeof(data1);

    int *data2 = data1;
    int size2 = sizeof(data2);

    int size3 = GetSize(data1);

    cout<<size1<<" "<<size2<<" "<<size3<<endl;
    return 0;
}

The results are as follows: 
write picture description here 
  data1 is an array, and sizeof(data1) is the size of the array. This array contains 5 integers, each of which occupies 4 bytes, since there are 20 bytes in total. data2 is declared as a pointer, even though it points to the array data1, the sizeof of the serious pointer is all 4. In C/C++, when an array is passed as a function parameter, the array is automatically degraded to a pointer of the same type. So even though the parameter data of the function GetSize is declared as an array, it degenerates to a pointer, and the result of size3 is still 4.

2. Two special properties of arrays

(1) Copy and assignment are not allowed

  You cannot copy the contents of an array to another array as its initial value, and you cannot use an array to assign values ​​to other arrays.

int a[] = {0,1,2}; // array of three integers
int s2[] = a; // error: initializing one array with another is not allowed
a2 = a; // error: cannot assign an array directly to another array

(2) Using an array is usually converted into a pointer

  In the C++ language, pointers and arrays are very closely related. When using an array, the compiler generally converts it to a pointer.

  Usually, the address character is used to obtain a pointer to an object, but the address character can also be used for any object. Elements of an array are also objects. Use the subscript operator on an array to get the element at the specified position in the array. So just like any other object, using the address notation on an element of an array can be used with a pointer to that element:

string nums[] = {"one", "two", "three"}; // array elements are string objects
string *p = &nums[0]; // p points to the first element of nums

However, arrays have another feature: in many places where an array name is used, the compiler automatically replaces it with a pointer to the first element of the array:

string *p2 = nums; // equivalent to p2 = &nums[0]

In most expressions, using an object of type array actually uses a pointer to the first element of the array.

Three, array parameters

  Two special properties of arrays affect how we define and use functions that act on arrays. Because arrays cannot be copied, we cannot use array parameters by value. Because arrays are converted to pointers, when we pass an array to a function, we are actually passing a pointer to the first element of the array. 
  Although we can't pass arrays by value, we can write parameters like arrays:

// Although the forms are different, these three print functions are equivalent
//Every function has a parameter of type const int*
void print(const int*);
void print(const int[]);
//The dimension here indicates how many elements we expect the array to contain, not necessarily
void print(const int[10]);

 Despite the different representations, the three functions above are equivalent: the only form of each function is of type const int*. When the compiler handles the call to the print function, it only checks if the passed argument is of type const int*:

int i=0,j[2] = {0,1};
print(&i); //correct: &i is of type int*
print(j); //correct: j is converted to int* and points to j[0]

 If we pass an array to the print function, the actual parameter is automatically converted into a pointer to the first element of the array, and the size of the array has no effect on the function call.

  Like other code that uses arrays, functions that take arrays as parameters must ensure that arrays are not out of bounds.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324811036&siteId=291194637