Chinaunix首页 | 论坛 | 博客
  • 博客访问: 501469
  • 博文数量: 92
  • 博客积分: 3146
  • 博客等级: 中校
  • 技术积分: 2314
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-27 10:20
文章分类

全部博文(92)

文章存档

2014年(3)

2013年(17)

2012年(16)

2011年(22)

2010年(34)

分类: C/C++

2010-09-27 10:56:02

/*
#include "string.h"
#include
#include
void getmemory(char **p) //函数的参数是局部变量,在这里给它分配内存还在,但是P释放了。
{
 *p=(char *) malloc(100);
 
 (*p)[0]='a';
 (*p)[1]='\0';
}
int main( )
{  
 char a[10]="eee";
 char *str=a;
 getmemory(&str);//传的 是指针的副本
 
 //strcpy(str,"hello world");
 printf("%s\n",str);
// free(str);
 return 0;
}
//要改变的是指针的指向,1  要传指针的指针。*/
 
#include "string.h"
#include
#include
void getmemory(char *&p) //函数的参数是局部变量,在这里给它分配内存还在,但是P释放了。
{
 p=(char *) malloc(100);
 
 (p)[0]='a';
 (p)[1]='\0';
}
int main( )
{  
 char a[10]="eee";
 char *str=a;
 getmemory(str);//传的 是指针的副本
 
 //strcpy(str,"hello world");
 printf("%s\n",str);
 // free(str);
 return 0;
}
//要改变的是指针的指向,2 要传指针引用。
 
 
/*第三种方法-----引用调用参数传递 */
//  add中,形参c是引用类型变量,实质上c就是main中的sum的一个别名
//  add中修改c的值,就是修改了main中的sum的值
//  return 调用执行得是否ok

#include
#define OK 1
typedef int status;
/*
status add(int a,int b,int c);
main()
{
    int a=2,b=3,sum=0;
    add(a,b,sum);
    printf("%d\n",sum);
}
status add(int a,int b,int c)
{
    c=a+b;
 
    return OK;
}
//传变量,只是传一个副本,不能改变变量的原来的值,
*/
 

/*
status add(int a,int b,int &c);
main()
{
    int a=2,b=3,sum=0;
    add(a,b,sum);
    printf("%d\n",sum);
}
status add(int a,int b,int &c)
{
    c=a+b;
 
    return OK;
}
////传变量的引用是变量本身,能改变变量的原来的值*/
 
/*
status add(int a,int b,int *c);
main()
{
    int a=2,b=3,sum=0;
    add(a,b,&sum);
    printf("%d\n",sum);
}
status add(int a,int b,int *c)
{
    *c=a+b;
 
    return OK;
}
//传变量的地址能改变变量的原来的值。==可以和引用互相替换*/
 

/*
status add(int a,int b,int *c);
main()
{
    int a=2,b=3,sum=0;
    add(a,b,&sum);
    printf("%d\n",sum);
}
status add(int a,int b,int *c)
{
    // *c=a+b;
 int d=6;
 c=&d;
 
    return OK;
}
//传变量的地址能改变变量的原来的值。==可以和引用互相替换*/
 
阅读(1155) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~