C language-pass value, pass address (pointer), transmit reference difference and contact

Source: Internet
Author: User

Many programmers do not know the difference between the value, the address, the reference, and today I will take a moment to introduce the essence of these concepts again.

In fact, not divided into three categories, only two categories can be. Pass the value and pass the reference. Why is there a pass-through address (that is, a pointer)? The essence is that we all agree on the concept of the value of the transfer and the interpretation of the error caused by the understanding of the concept of pointers.

Pointer: (Simple to add) is actually very simple, the pointer is a variable, if it is not to be said to be a special variable is not too, because the pointer of the initialization and the reference and other different modes of operation. In terms of the memory distribution, the pointer and a variable are stored in memory without any difference, but the pointer holds the address of the variable, it is so simple.

Defined:

Pass value: Many people may have heard that the value of a pass is nothing more than a copy of the argument passed to the formal parameter. This sentence is not wrong, but the understanding is still a bit abstract. In a word, the value of the pass is to assign the argument to the formal parameter, and the argument is not associated with the formal parameter, and the modification of the parameter will not affect the argument.

Address: Why the address is also a kind of transmission value? Because the pass-through address is a copy of the address of the argument is passed to the formal parameter. Or a word, the address is to copy the address of the actual parameter to the formal parameter. The address of the argument is not associated with the address of the formal parameter when the copy is complete, and the modification of the argument address does not affect the argument, but the modification to the object pointed to by the parameter address is directly reflected in the argument, because the object pointed to by the formal parameter is the object of the formal parameter.

Reference: A reference is not a copy of any actual argument, and one sentence is to have another variable execute that argument as well. Is two variables that point to the same object. This is the modification of the formal parameter, which must be reflected on the actual parameter.

Difference:

(1): The effect of passing a reference to a function is the same as passing a pointer. At this point, the parameter of the function is used as the real parametric or an alias of the object in the original central melody function, so the operation of the parameter variable in the function is the operation of its corresponding target object (in the central Melody function).

(2): Using the parameters of the reference transfer function, in memory does not produce a copy of the argument, it is directly to the actual parameter operation, and the use of the general variable transfer function parameters, when a function call occurs, you need to assign a storage unit to the parameter, the parameter is a copy of the argument variable; Therefore, when the parameter passes the data is large, uses the reference to pass the parameter with the general variable efficiency and occupies the space to be good.

(3): The use of pointers as parameters of the function, although can also achieve with the use of reference, but in the function of the parameter allocation of the same storage unit, and the need to re-use "* pointer variable name" in the form of operations, which is prone to errors and poor program reading; On the other hand, at the call point of the keynote You must use the address of the variable as the argument. And references are easier to use and clearer.


Essence: Both the pass-through and the pass-through are value-transfer methods, except for the reference


















C language-pass value, pass address (pointer), transmit reference difference and contact

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.