指针与内存

# include "stdio.h"
# include "iostream.h"
# include "malloc.h"
# include "string.h"
//关于函数赋值
void fun1(int num1,int num2)
{
    cout<<"fun1():"<"num1="<"  "<<"num2="<int temp=num1;
    num1=num2;
    num2=temp;
    cout<<"num1="<"  "<<"num2="<int *p1,int *p2)
{
    cout<<"fun2():"<"*p1="<<*p1<<"  "<<"*p2="<<*p2<int * pt=p1;
    p1=p2;
    p2=pt;
    cout<<"*p1="<<*p1<<"  "<<"*p2="<<*p2<int *p1,int *p2)
{
    cout<<"fun2():"<"*p1="<<*p1<<"  "<<"*p2="<<*p2<int temp=*p1;
    *p1=*p2;
    *p2=temp;
    cout<<"*p1="<<*p1<<"  "<<"*p2="<<*p2<*编译成功但是程序存在问题,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="<"  "<<"b="<//  fun2(&a,&b);
//  cout<<"执行fun2()"<<"a="<"  "<<"b="<//  fun3(&a,&b);
//  cout<<"执行fun3()"<<"a="<"  "<<"b="<

你可能感兴趣的:(指针,C++)