A little note on function handling arrays in C + +

Source: Internet
Author: User

PointDemo0905.cpp: Defines the entry point of the console application.
//

#include "stdafx.h"

Notation 1
int sum (int array[],int num);
Notation 2
int sum (const int *array,int num);

void revalue (int r,int *array,int num);

int _tmain (int argc, _tchar* argv[])
{
int adata[8]={1,2,3,4,5,6,7,8};
Revalue (2,adata,8);
int result=sum (adata,8);
return result;
}

Notation 1, when and only when used for function headers or function prototypes, int array[] and int *array mean the same
int sum (int array[],int num)
//{
int total=0;
for (int i=0;i<num;i++)
Total=total+array[i];
return total;
//}

22 Ways to do this function is the same as the effect
int sum (const int *array,int num)
{
int total=0;
for (int i=0;i<num;i++)
{
* (array+i) =10* (* (array+i)); Because the function header declares a const, the statement will error
total=total+ * (array+i);
}
return total;
}

modifying array data
void revalue (int r,int *array,int num)
{//Because the array needs to be modified so declare int *array instead of const int *array
for (int i=0;i<num;i++)
* (array+i) =r* (* (array+i));

}

/**
1, the array in C + + as a function parameter, is the first address, type, number of the array to inform the function, and then the function array processing
2. To tell the function of the array address and number, pass through two different parameters
int sum (int *array,int num) or/int sum (int array[],int num)
Instead of trying to pass Sun (int array[size])
3, the use of ordinary parameters, because C + + by value, using a copy of the data, so it does not break (modify) the data, but the function of the array address or pointer to use the original data, directly access the memory unit, is able to modify the data.
4. If you need to avoid inadvertently modifying the original data (when you accept pointer arguments), you can use the keyword const when declaring parameters
such as int sum (const int array[],int num), const simply means that it is not possible to do an action like array[0]++ in the SUM function to modify array[], not to say that the argument adata when calling sum in main must be a constant.
Constants are defined and can never be modified until the program exits memory destruction, where the adata data is guaranteed not to be modified when the SUM function is executed in such an int sum (const int array[],int num).

Bottom-up programming, from component to overall operation, this approach is suitable for OOP programming, with the first emphasis on data representation and manipulation.


5, in C + +, the function of the array, the type of the array, the starting position, the number must be submitted to it, there are two ways
1) Traditional method, fun (int *array,num) ... All the information in the array can be obtained by the array starting position and number
2) Specify the element interval method, fun (int *begin,int *end) ... The information provided by these two pointers also allows for full-fledged operation of the array

6. If the data type itself is not a pointer, you can assign the address of a const or non-const type of data to a pointer of the const type, but only the address of the data of the non-CONSTL type is assigned to a pointer of the non-const type.
such as const int a=0;
int b;
const int *p; Just say the pointer type is constant
int *ptr;
p=&a; p=&b; ptr=&b; Valid
ptr=&a; Invalid


*/

A little note on function handling arrays 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.