Several problems of C + + array as function parameter

Source: Internet
Author: User

This article needs to address 2 questions about arrays in C + +:
1. Array as function parameter, pass value or address?
2. Can the number of array elements in a function parameter be determined?


Look at the following code first.

#include <iostream>using namespacestd;voidTestarrayarg (inta[]) {cout<<Endl; cout<<"In func ..."<<Endl; cout<<"Array Address:"<< a <<Endl; cout<<"Array Size:"<<sizeof(a) <<Endl; cout<<"array element count:"<<sizeof(a)/sizeof(a[0]) <<Endl; cout<<"changing the 4th element ' s value to ten."<<Endl; a[3] =Ten;}intMain () {intA[] = {1,2,3,4,5}; cout<<"In main ..."<<Endl; cout<<"Array Address:"<< a <<Endl; cout<<"Array Size:"<<sizeof(a) <<Endl; cout<<"array element count:"<<sizeof(a)/sizeof(a[0]) <<Endl;  Testarrayarg (a); cout<< Endl <<"The 4th element ' s value:"<< a[3] <<Endl; return 0;}

The results of the operation are as follows:

In main ...
Array address:0012ff4c
Array size:20
Array element Count:5

In Func ...
Array address:0012ff4c
Array Size:4
Array element count:1
Changing the 4th element ' s value to 10.

The 4th element ' s Value:10

When we call Testarrayarg () directly with array A as a parameter, the address of the actual participating parameter is 0012FF4C. Also, after modifying the value of a[3] to 10 in Testarrayarg (), the value of a[3] returned in the main () function has changed. These all indicate that arrays in C + + are addressed as function parameters .

It is important to note that in main (), the size of the array can be determined.

Array size:20
Array element Count:5

However, when passed as a function parameter, its size information is lost, leaving only the information of the first element in the array.

Array Size:4
Array element count:1

This is because C + + actually passes the array as a pointer, which points to the first element of the array. The function passing mechanism of C + + is not responsible for the end of the following array.

The above attribute can be summarized as the size can be determined only within the scope of the domain in which the array is defined .

Therefore, if you access each element of an array in a function that accepts array parameters, you pass the array size as another auxiliary parameter in the domain scope that defines the array. Then there is another function defined as follows:

void testArrayArg2 (intint  arraylength) {  "" 1] << Endl;}

This can be called in Main ():

TestArrayArg2 (A, sizeof (a)/sizeof (a[0]));

In this way, the array elements are safely accessed in testArrayArg2 ().

Transferred from: http://blog.csdn.net/sarkuya/article/details/6554538

Several problems of C + + array as function parameter (GO)

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.