C++

Source: Internet
Author: User

function Passing Value

Example 1:

1#include <iostream>2#include <string>3 using namespacestd;4 intMain () {5    stringstr1="I Love china!", str2="I Love jinan!";6    voidExchange (string*P1,string*p2);7cout<<"str1:"<<str1<<Endl;8cout<<"str2:"<<str2<<Endl;9Exchange (&str1,&str2);Ten    //take str1 and Str2 's address to P1 and P2 Onecout<<"str1:"<<str1<<Endl; Acout<<"str2:"<<str2<<Endl; -    return 0; - } the voidExchange (string*P1,string*p2) { -  string*P3; -P3=P1;//P1 gave the address to P3. -P1=P2;//P2 gave the address to P1. +P2=P3;//P3 gave the address to P2. -  //The result is that P1/P2/P3 addresses each other and does not affect the value of STR1/STR2 +}

1#include <iostream>2#include <string>3 using namespacestd;4 intMain () {5    stringstr1="I Love china!", str2="I Love jinan!";6    voidExchange (string*P1,string*p2);7cout<<"str1:"<<str1<<Endl;8cout<<"str2:"<<str2<<Endl;9Exchange (&str1,&str2);Ten    //take str1 and Str2 's address to P1 and P2 Onecout<<"str1:"<<str1<<Endl; Acout<<"str2:"<<str2<<Endl; -    return 0; - } the voidExchange (string*P1,string*p2) { -  stringP3; -P3=*P1;//assign the value of the str1 that P1 refers to P3 -*P1=*P2;//assign the value of the str2 that P2 refers to P1 +*P2=P3;//assign the value of P3 to the str2 of P2 -}

Example 2:

1#include <iostream>2#include <string>3 using namespacestd;4 5 //Output p=26 voidfunctionintp1)7 {8p1=5;9 }Ten  One intMain () A { -     intp=2; - function (p); thecout<<p<<Endl; -     return 0; -}

1#include <iostream>2#include <string>3 using namespacestd;4 5 //Output p=56 voidfunctionint*p1)7 {8*p1=5;9 }Ten  One intMain () A { -     intp=2; -Function (&p); thecout<<p<<Endl; -     return 0; -}

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.