|
发表于 2020-4-10 00:36:29
|
显示全部楼层
本帖最后由 howzyao 于 2020-4-10 00:38 编辑
- #include <cstdlib>
- #include <iostream>
- #include <stdio.h>
- using namespace std;
- int fun(int n)
- {
- int * p;
- cout<<"fun中的p没有赋给左值地址:\n";
- cout<<"1---p的地址:"<<p<<endl;
- p= (int * )malloc(sizeof(int));
- cout<<"2---p的地址:"<<p<<" 值"<<*p <<" (右值---来自强制转换)"<<endl;
- *p=n;
- cout<<"3---p的地址:"<<p<<" 值"<<*p<<" (右值---来自参数)"<<endl;
-
- cout<<"4---fun中的p,在每调用fun函数时,向调用函数传回了不同地址的p"<<endl;
-
- cout<<"-----------------------------------"<<endl;
- return * p;
- }
-
-
- int main(int argc, char *argv[])
- {
-
- int a;
- printf("%s %d %s","a没有赋值,现在是随机值:", a ,"\n");
- a=fun(10);
- cout<<"a=fun(10)语句,由于fun返回的地址中的值,使a得到了参数10的值"<<endl;
- printf("%d,%s,%d", a+fun(10),"a没有被初始化,但被上一语句: a=fun(10) 所赋值,值=",a,",\n" );
-
-
- system("PAUSE");
- return EXIT_SUCCESS;
- }
复制代码 |
|