# include "stdio.h"
# include "iostream.h"
# include "malloc.h"
# include "string.h"
//关于函数赋值
void fun1(int num1,int num2)
{
cout<<"fun1():"<<endl<<"num1="<<num1<<" "<<"num2="<<num2<<endl;
int temp=num1;
num1=num2;
num2=temp;
cout<<"num1="<<num1<<" "<<"num2="<<num2<<endl;
}
void fun2(int *p1,int *p2)
{
cout<<"fun2():"<<endl<<"*p1="<<*p1<<" "<<"*p2="<<*p2<<endl;
int * pt=p1;
p1=p2;
p2=pt;
cout<<"*p1="<<*p1<<" "<<"*p2="<<*p2<<endl;
}
void fun3(int *p1,int *p2)
{
cout<<"fun2():"<<endl<<"*p1="<<*p1<<" "<<"*p2="<<*p2<<endl;
int temp=*p1;
*p1=*p2;
*p2=temp;
cout<<"*p1="<<*p1<<" "<<"*p2="<<*p2<<endl;
}
//关于内存
/*编译成功但是程序存在问题,p指针指向的内存空间没有被释放(因为在堆空间中)
要释放free(p)。*/
void mem1()
{
char * p=(char *)malloc(20);
strcpy(p,"hello");
printf("*p=%s\n",p);
//cout<<*p;只输出h
free(p);
}
void main()
{
int a=10,b=20;
// fun1(a,b);
// cout<<"执行fun1()"<<"a="<<a<<" "<<"b="<<b<<endl;
// fun2(&a,&b);
// cout<<"执行fun2()"<<"a="<<a<<" "<<"b="<<b<<endl;
// fun3(&a,&b);
// cout<<"执行fun3()"<<"a="<<a<<" "<<"b="<<b<<endl;
mem1();
}
指针与内存
最新推荐文章于 2017-09-10 10:40:28 发布