The benefits of using pointers for function parameters,

Source: Internet
Author: User

The benefits of using pointers for function parameters,

The first step is to understand the process of function parameter transfer, which is a copy type, for example

void Modify (int a)

{

a++;

}

void Main ()

{

int a=5;

Modify (a);

printf ("%d", a);

}

After the execution of the program, the value of a is still 5, for what, because in the execution of the Modify function, is another open storage space, the value of a copy of the past, and then the Modify function to do all the operation is for this newly opened space, so after the execution of the program, The value of a does not change,

If we use pointers to pass parameters, it's a whole different time,

void Modify (int * a)

{

(*a) + +;

}

void Main ()

{

int a=5;

Modify (&a);

printf ("%d", a);

}

The execution of the program is of course 6, because you pass the address of a, so the Modify function is a address above the contents of the operation, with the pointer as a function parameter, when the parameter is actually also opened up a storage space, and then the value of the pointer copied past, The function then operates on this pointer variable inside the newly opened storage space.

When to use the pointer, when the normal copy of the way to pass the parameters, when the parameters passed is a structure, and the structure is relatively large, this time it is best to use a pointer, if the copy, the only time spent in the copy will be wasted,

The benefits of using pointers for function parameters,

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.