C + + array lengths and array names passed as parameters to functions

Source: Internet
Author: User

In looking at the sort, the first is the insertion sort, the idea is sorted out and then you want to implement the code, and then the question comes:

  How do I find the array length?

If you remember correctly, there should be a direct available method in Java, and Python (the sequence) also has a. Len, which has a strlen () (# include <string.h>)


One way is to use sizeof ()

One
First define the array
int array[] = {7,1};

I didn't even think about it at first, just in the sub-function.
int array_length (int  a[]) {    intsizeof(a)/sizeof(a[0 ]);         return Len;}
However, the result of the call in the main function is not 5, but 8.


If the code in the function is used directly in the main function, the correct answer can be obtained 5
 #include <iostream>int  Main (int  argc, const  char  * argv[]) { int  array[]={18 , 10 , 15 , 7 , 1     };  int  len = sizeof  (Array)/sizeof  (A[0   <<len<<endl;}  

Change the number and content of array elements, or so. For example, if the array element is increased to 6, if the child function is used, then 8 is obtained, and if sizeof () is used directly in the main function, 6

Strange, the same code, in main () good, to the sub-function inside why not?


Two

This article states:
The argument is passed by assigning the value of the argument to the parameter. However, for arrays is an exception, because the array of data too much, its one by one assignment is cumbersome and wasted space, so the array as a parameter to the function is just the address of the first element of the array, the function in the need to use the following elements and then follow the address and array subscript to find . That is to say, the elements in the back are not in the function, so it is not surprising to find the size of the group in the function.
/* * * * * * So write the form parameter can be int array_length (int a[]) {    return 0;}  */******* can also be int array_length (int *a) {return 0;} *******/


At this point (in the child function) with sizeof (a), then get the length of the pointer (here "length of the pointer" This expression may be problematic, how to accurately express it?) ), because it is a 64-bit machine, so it is 8 (bytes)
(sizeof (a[0]) or 4, where a[] is an array of type int)

On the other hand, another operation in the sub-function, such as the a++ of the pointer with the array name, or the same as in main ()
#include <iostream>voidLengthinta[]) {cout<<sizeof(a[0]) <<Endl; cout<<*a++<<Endl; cout<<*a++<<Endl; cout<<*a++<<Endl;}intMainintargcConst Char*argv[]) {        inta[]={ $, the, the, the,1,1,2,Ten, the,7,1};    Length (a); return 0}//The result of the output is//4// $// the// the
The output shows that the child function is incremented by the first address and the length of the array type.
In the case of sorting, the contents of the address are modified in the sub-function, and the contents of the main function are changed, that is, the data in memory is manipulated, and the actual parameters are affected. So you can not return an array or something, directly with the void type of sub-function is good.


Finally, the solution, or as this article says:
In general, the array length is first calculated outside, and then passed in as parameters.

As for the method of macro definition/class, we will study
Add a sentence, can use Google do not use degrees Niang, search quality is not the 1:30 point, save time is not 1:30 point.



Report:
1). How the Mac is displayed is a 32-bit/64-bit machine:
32-bit machine pointer Why is the difference between 4 bytes 64 bit and 32 bit machine

C + + array lengths and array names passed as parameters to functions

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.