Array as function parameters and pointer as function parameters

Source: Internet
Author: User

There is still a difference

Although most of the time they can be used with each other, the biggest difference is that the pointer can be changed, you can change the value pointing to the content through the pointer, but the array name is not

For example: (as parameters respectively)

Int num [10], * P;

P = num; // P points to the first element of the array.

Call two functions in the main function

1 sum (Num );

2 sum (p + 2, 10 );

This is different because the address at the beginning of the num array cannot be changed, but P points to the third element of the array, and the 10 at the end can also be changed.When using a pointer as a form parameter, we need to include an int n to indicate the number of data to be processed.So using a pointer as a form parameter will give you an illusion that it is indeed an element of the array to which it points (it depends on the movement of your pointer, such as P + 2, etc, array names cannot be moved), but it is just a pointer. You can use sizeof (Num) and sizeof (p) to evaluate their values in the called function, I know why I always add an int.
N, because if n is not added, the function does not know the number of the numbers you want to operate. In addition, do not forget to pass the array and pass the General int, double, and other types,If an array is passed, the original array (problematic) is passed, and the function operation changes the original array, while the passing of a general number is only a copy of it, which has no effect on the original number.;

Related Article

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.