Problem of passing array parameters in C + +

Source: Internet
Author: User

I am so very food, originally I was going to do a good thing today dynamic_programming a series of classic problems, the result is just stay in the array to understand the principle of the connection.

Reason:

1. When I want to do a hands-on implementation, in order to give the function and variable a professional name, deliberately to see some of the details of the naming specification problem;

2. Yo West, the naming specification solves, but how to take the name to look like a bit taller, not a good code does not need comments, just look at the name of the line. Then I found that I want to express the variable name corresponding to the word I do not, so I went to check the words;

3. Yo West +1, the name is finally done, but this reference code inside, the transfer parameters why with the vector, the last time with the vector, I switched to the array bar. Then I found that I was not familiar with a series of questions about array parameters. Then I began to write some small functions to test the parameters of the array to go to the problem;

4. Now, the code is not implemented, OJ also did not brush, then put the array to the problem of the argument first write down it ...

To pass an array as a parameter

There are two methods of transfer, one is function (int a[]); The other is function (int *a)

Both of these two methods modify the array parameter in the function to affect the value of the argument itself!

For the first, according to the previous study, the formal parameter is a copy of the argument, which is a local variable. But the array is an exception, because the array of data too much, and its one by one assignment is cumbersome and wasted space, so the array as a parameter to the function is only the address of the first element of the array, the data is in memory , the function in the need to use the following elements and then follow the address and array subscript memory to find. This means that the elements behind it are not in the function at all. Therefore, it is not possible to use sizeof in the test () function to find the size of the array, must be outside the good and then pass in.

For the second, it is a call to the address, no need to say.

It is also important to note that a const before the formal parameters of the two functions above indicates that the entire array is read -only and not only the data stored for the first address is read-only.

The test code is as follows:

#include <stdio.h> #include <algorithm>using namespace std;void test1 (int[], int size), void test2 (int *p, int size);//void test2 (const int *p, int size); int main (void) {    int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, ten};    int size = sizeof (a)/sizeof (int);    /* The value printed here is equal to the P value in Test1 (), test2 ()     * i.e. the address value *    /printf ("%p \ n", a);    Test1 (A, size);    Test2 (A, size);    int i;    printf ("main:");    for (i = 0; i < size; ++i)    {        printf ("%d", A[i]);}    } void test1 (int p[], int size) {    printf ("%p \ n", p);    P[4] = 111;    printf ("test1:");    int i;    for (i = 0; i < size; ++i)    {        printf ("%d", P[i]);    }    printf ("\ n");} void test2 (int *p, int size) {     printf ("%p \ n", p);    * (p+4) = 222;    printf ("test2:");    int i;    for (i = 0; i < size; ++i)    {        printf ("%d", * (P+i));    }    printf ("\ n");}

  

Summarize:

This basic knowledge of oneself is really catch the urgency Ah! English is terrible! Usually more knock code it! Read more English books!! Instead of copying code!! To original original!!

Ya know?! Less snacking, more coding is the truth!

Problem of passing array parameters in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.