基础知识2、引用方式调用函数--请输出结果


#include <stdio.h>

//申明后面要用的函数
void swap(int *x,int *y);

int main ()

{
   /* 局部变量定义 */
   int a = 100;
   int b = 200;

   printf("交换前,a 的值: %dn", a );
   printf("交换前,b 的值: %dn", b );

   /* 调用函数来交换值
    * &a 表示指向 a 的指针,即变量 a 的地址
    * &b 表示指向 b 的指针,即变量 b 的地址
   */
   swap(&a, &b);

   printf("交换后,a 的值: %dn", a );
   printf("交换后,b 的值: %dn", b );

   return 0;
}

/* 函数定义 */
void swap(int *x, int *y)
{
   int temp;
   temp = *x;    /* 保存地址 x 的值 */
   *x = *y;      /* 把 y 赋值给 x */
   *y = temp;    /* 把 temp 赋值给 y */

   return;
}

 

posted @ 2017-12-11 09:05  凤凰汇郭  阅读(288)  评论(0编辑  收藏  举报