// Example program #include<iostream> #include<string> inttest(int a, int b) { std::cout<<"a dizhi: " <<&a <<" b dizhi :"<<&b<<std::endl; int c = a+b; std::cout<<" c dizhi: " <<&c<<std::endl; return c; }
intmain() { int i = 3; int j = 4; std::cout<<" i dizhi: " <<&i <<" j dizhi :"<<&j<<std::endl; int g = test(i ,j); std::cout<<" g dizhi: " <<&g<<std::endl; }
结果
1 2 3 4
i dizhi:0x722a4acd7314 j dizhi :0x722a4acd7318 function :a dizhi:0x722a4acd72dc b dizhi :0x722a4acd72d8 function :c dizhi:0x722a4acd72ec g dizhi:0x722a4acd731c
// Example program #include<iostream> #include<string>
inttestA(void) { int b = 1 ; return b ; } char * testB(void) { char str[] = "abc" ; return str ; //但这样的方法是不推荐的 } intmain() { printf( " the value of testA is %d \n", testA() ) ; printf( " the value of testB is %c ", *( testB() ) ) ; }
对于返回值的情况:
testA与main函数同在栈区,testA结束时C++创建临时变量,然后将返回值复制给该临时变量。printf( " the value of testA is %d \n", testA() ) 时输出的是该临时变量的值,testA中的b已经不存在。