C/C++中传值和传地址,引用

  C/C++中参数传递有两种方式,传值或传地址(传引用),通常我们要在被调用函数中改变一个变量的值就需要传地址调用方式,例如:

void swap_by_value(int a, int b)
{
    int temp;
    temp = a;
    a = b;
    b = temp;
}
void swap_by_ptr(int* pa, int* pb)
{
    int temp;
    temp = *pa;
    *pa = *pb;
    *pb = temp;
}
int main(int argc, char* argv[])
{
    int a=5, b=6;
    swap_by_value(a, b);//传值
    printf("a=%d, b=%d\n", a, b);//a=5,b=6
    swap_by_ptr(&a, &b);//传地址
    printf("a=%d, b=%d\n", a, b);//a=6,b=5
    return 0;
}

  很显然,通过传值调用并不能改变main函数中的a与b,要想改变a与b的值就必须通过传地址方式,然后在被调用函数中通过指针访问到主函数中的变量。

  可是通过传递变量指针就一定是传地址调用么?看下面一个例子:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void get_str(char* p);

int main(int argc, char* argv[])
{
    char* p = NULL;
    get_str(p);//传递指针
    printf("p=%p\n", p);
    puts(p);
    return 0;
}
void get_str(char* p)
{
    p = (char*)malloc(sizeof("abcd"));
    printf("%d\n", sizeof("abcd"));
    strcpy(p, "abcd");
    return;
}

  其中参数p是一个指针,程序想通过p来得到在get_str(char* p)中分配的一块内存。但是上面的代码并不能完成此功能。原因是程序需要在get_str(char* p)中修改main函数中的指针变量p,使其指向malloc返回的一块内存,要想在 get_str(char* p)中修改main函数中p的值就只有把p的地址传过去,在get_str中通过地址访问主函数中的变量p。正确的代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void get_str(char** p);

int main(int argc, char* argv[])
{
    char* p = NULL;
    get_str(&p);//传递地址(引用)
    printf("p=%p\n", p);//abcd
    puts(p);
    return 0;
}
void get_str(char** p)
{
    *p = (char*)malloc(sizeof("abcd"));
    printf("%d\n", sizeof("abcd"));
    strcpy(*p, "abcd");
    return;
}

  引用是C++中的语法,结合了传地址和传值的优点,即传递的是一个地址,但在形式上又没有指针那个怪异的符合*,看上去舒服多了。

下面是一个传引用版的swap()函数

void swap_by_ref(int& a, int& b)
{
  int temp;
  temp = a;
  a = b;
  b = temp;
}
int main(int argc, char* argv[])
{
  int a=5, b=6;
  swap_by_ref(a, b);
  printf("a=%d,b=%d \n",a,b);//a=6,b=5
  return 0;
}

  总之,如果想在被调用函数中改变哪个变量的值就需要把那个变量的地址传给被调用函数,这样在被调用函数中通过地址就可以访问和修改这个变量。传值的时候会对参数进行拷贝,然后传给被调用函数,而传地址只是对地址进行一份拷贝,然后赋给形参,这样在C++中如果传递一个比较大的对象时效率就有所区别了。对于简单类型的变量来说传值和传地址在根本上是没有区别的,都是传递一个数给被调用函数,而怎样去解释和使用这个数是我们事先约定好的。

转自:http://blog.chinaunix.net/uid-21411227-id-1826826.html

2015-03-14 10:44:10