C array passing problem, C array parameter passing problem

Updated on technology 2024-02-09
8 answers
  1. Anonymous users2024-02-05

    2D array address - > row address - > column address (variable address) - > variable value, or excerpt a description for you.

    Let a two-dimensional array be defined as:

    int a[4][3]=,4,5,6},6,5,4},3,2,1} So what do they mean? The analysis is shown in Figure 1. Let's start with the "degraded" effect of the subscript "[.

    Starting from the element value represented by a[2][0], a " becomes a[2] (the same as a[2]+0), and a[2] represents the column address of element a[2][0], and here the element value is "upgraded" to a column address by removing a " Remove a[2] and change it to a 2, which means the row address of line 2, and here you can "promote" the column address to the row address by removing ".

    Reanalyze the "degraded" effect of the operator "*". a2 represents the row address of line 2, and *(a+2) represents the column address of the first column element of row 2. **(a2) represents the element value a[2][0], where the two "*s" make the row address a+2 drop two levels in a row until the address meaning disappears and becomes the element value.

  2. Anonymous users2024-02-04

    The old man occupies a answering position and does a task. The landlord is not strange.

  3. Anonymous users2024-02-03

    I wanted to write a utility library with a method to get the length of an array, a simple sizeof(), but I stepped on a pit.

    In the main() function, the data obtained by calling the assigned array directly is correct. However, if you pass the array directly as an argument to another function that is called, and get the length of the array in this function, the program will only calculate the length of the first element of the entire array.

    It turns out that the array data transmitted by the function parameter method in C++ only contains the first element of the array, that is, in the view of the function that obtains the parameter, the parameter has only one element, not a long array.

    But since the data type is still an array, we have no problem manipulating the array directly through subscripts.

    We can define a function by adding a "&"int function(string &a)) to the end of the parameter's type name, and we can use the pointer "*a" (function(*a)) when calling the function. Now we can work on where the function is defined.

    In this function, cout "Actually, this method doesn't work, because it doesn't solve my above problem.

    Function definition: function(string &a){}

    function call: function(*a);

    Function definition: function(string &a){}

    function call: function(*a);

    Pass in an array (both of the above methods) and pass the length of the array to the calling function, that is, the size of the array will be solved outside the called function, and it should not be a problem.

  4. Anonymous users2024-02-02

    1. The transfer of the array in the function is carried out in the form of a pointer, as a practical argument, the modulated function only receives the first address of the array, and the length of the array often needs to be provided by another practical argument (except for the character array), 2. If you want to pass (return) the array defined in the modulated function to the calling function, you need to dynamically apply for (new) memory, and still return in the form of a pointer, the length does not need to be returned, but you should remember to release (delete) the applied dynamic space before the end of the program.

  5. Anonymous users2024-02-01

    Array name + array size. That's the way it can be.

    int a[10];

    getarray( int b, int length);

    2.It is risky to have a new or malloc new array in the subfunction, but this is not supported.

    getarray( int* b, int length);

    4.An array can be referenced, but as a result, the result is only the address of the first element of the array.

  6. Anonymous users2024-01-31

    1.Directly pass the first address of the array, that is, the variable name of the array;

    2.It's better to use new allocate, delete free in the main function;

    3.The address at the beginning of the array is the pointer, and of course the length is passed if necessary;

    4.Even if there is a pointer and a reference, it looks messy, but it should be possible to use the pointer reference.

  7. Anonymous users2024-01-30

    Arrays are passed in the form of pointers in C++.

    We can write the parameters as pointers or arrays, but essentially they pass pointers. To quote an example from C++ Primer:

    Although the form is different, the three print functions are equivalent.

    Each function has a const int* parameter.

    void print(const int*);

    void print(const int);As you can see, the intent of the function is to act on an array.

    void print(const int[10]);The dimension here indicates how many elements we expect the array to contain, which is not necessarily true.

    The above three forms of declarations are equivalent, and when the print function is called, the compiler only checks whether the passed argument is (or can be converted to) const int*:

    int i = 0, j[2] = ;

    print(&i);Correct: The type of &i is int*

    print(j);Correct: j to int* and points to j[0].

  8. Anonymous users2024-01-29

    The passing of the array in the function is actually the first address of the array, as long as you understand this.

    A pointer is an address, a reference is an address...

Related questions
15 answers2024-02-09

Click on the karma, in the background button, the karma will turn to transfer value. >>>More

9 answers2024-02-09

Scope. You static char *chh;

static char *ch1;Although the address pointed to by the two pointers does not change, have you ever wondered whether the memory address they point to has been released, char chc[10]; It's local, the function is out, the lifecycle is over, and you're trying to access it with a pointer in void times(). >>>More

7 answers2024-02-09

If there is an exception or a null reference, it must be a problem with the program itself. >>>More

5 answers2024-02-09

1.When some software runs for the first time, you will be prompted to select an access point, either directly in the settings or in the settings of those networked software. >>>More