zoukankan      html  css  js  c++  java
  • C语言的传值与传址调用

    传值调用并不能改变两个变量的值,而传址能。

    为什么,因为,传值调用,仅仅是在函数内,调换参数的值。

    而地址所指向的值,改变的不仅仅是函数内,函数外也改变。

    请看代码:

    这里还要注意:通常我们不会返回局部变量的地址.

    /* ************************************************************************
    * Filename: main.cc
    * Description:
    * Version: 1.0
    * Created: 2011年12月14日 17时06分13秒
    * Revision: none
    * Compiler: gcc
    * Author: YOUR NAME (),
    * Company:
    * ***********************************************************************
    */

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

    void swap_value(int x,int y)
    {
    int temp;
    temp = x;
    x = y;
    y = temp;
    printf("传值函数内的输出 %d %d \n",x,y);
    }

    void swap_address(int *x,int *y)
    {
    int temp;
    temp = *x;
    *x = *y;
    *y=temp;
    printf("传址函数内的输出 %d %d \n",*x,*y);
    }

    int main(int argc, char *argv[])
    {
    int x = 10;
    int y = 0;

    printf("x y \n");
    printf("初值 %d %d \n",x,y);
    //传值子程序调用(交换xy)
    swap_value(x,y);
    printf("传值函数外调用 %d %d \n",x,y);

    //传地址字程序调用(交换x,y)
    swap_address(&x,&y);
    printf("传址函数外调用 %d %d \n",x,y);
    return 0;
    }



  • 相关阅读:
    HttpServlet
    Servlet练习和自定义GenericServlet实现类
    Servlet-ServletRequest
    HTTP协议-GET/POST请求
    Servlet-ServletConfig对象
    Servlet
    1089. Duplicate Zeros
    1002. Find Common Characters
    17. Letter Combinations of a Phone Number
    254. Factor Combinations
  • 原文地址:https://www.cnblogs.com/hnrainll/p/2288046.html
Copyright © 2011-2022 走看看