Value and pass-through address (reference)

Source: Internet
Author: User

There are two ways to pass a parameter in C + +, pass a value or pass an address (pass a reference), usually we need to change the value of a variable in the called function to call the address method, for example:

void Swap_by_value (int a, int b) {    int temp;    temp = A;    A = b;    b = temp;} void Swap_by_ptr (int* pa, int* pb) {    int temp;    temp = *PA;    *pa = *PB;    *PB = temp;} int main (int argc, char* argv[]) {    int a=5, b=6;    Swap_by_value (A, B);//Pass Value    printf ("a=%d, b=%d\n", A, b);//a=5,b=6    swap_by_ptr (&a, &b);//Address    printf ("a=%d, b=%d\n", A, b);//a=6,b=5    return 0;}

Obviously, passing a value call does not change the A and B in the main function, and if you want to change the value of a and B, you must pass the address method and then access the variable in the main function through pointers in the called function.

But by passing the variable pointer it must be a call to address? Take a look at the following example:

#include <stdio.h> #include <stdlib.h> #include <string.h>void get_str (char* p); int main (int argc, char* argv[]) {    char* p = NULL;    GET_STR (P);// pass pointer    printf ("p=%p\n", p);    Puts (p);    return 0;} void Get_str (char* p) {    p = (char*) malloc (sizeof ("ABCD"));    printf ("%d\n", sizeof ("ABCD"));    strcpy (P, "ABCD");    return;}

Where parameter p is a pointer, the program wants to use p to get a piece of memory allocated in GET_STR (char* p) . However, the above code does not complete this function. The reason is that the program needs to modify the pointer variable p in the main function in Get_str (char* p) to point to a piece of memory returned by malloc, in order to modify the value of P in the main function in Get_str (char* p), only the address of P is passed, in the get The variable p in the main function is accessed through the address in _str. The correct code is as follows:

#include <stdio.h> #include <stdlib.h> #include <string.h>void get_str (char** p); int main (int argc, char* argv[]) {    char* p = NULL;    Get_str (&p);// Pass address (Reference)    printf ("p=%p\n", p);//abcd    puts (p)    ; return 0;} void Get_str (char** p) {    *p = (char*) malloc (sizeof ("ABCD"));    printf ("%d\n", sizeof ("ABCD"));    strcpy (*p, "ABCD");    return;}

Reference is the syntax of C + +, combined with the advantages of the address and the value of the pass, that is, the delivery is an address, but in formal and no pointer that weird match *, looks more comfortable.

The following is a quoted version of the Swap () function

void Swap_by_ref (int& A, int& b) {int temp;  temp = A;  A = b; b = temp;}  int main (int argc, char* argv[]) {int a=5, b=6;  Swap_by_ref (A, b); printf ("a=%d,b=%d \ n", A, b);//a=6,b=5 return 0;}

In summary, if you want to change the value of which variable in the called function, you need to pass the address of that variable to the called function, so that the variable can be accessed and modified by the address in the called function. When a value is passed, it is copied to the called function, and the address is simply a copy of the address and then assigned to the parameter, so that in C + + the efficiency is different when passing a larger object. For a simple type of variable, the value of the transfer and the address is fundamentally no difference, is to pass a number to the called function, and how to interpret and use this number is our prior agreement.

Transferred from: http://blog.chinaunix.net/uid-21411227-id-1826826.html

2015-03-14 10:44:10

Value and pass-through address (reference)

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.