> For the complete documentation index, see [llms.txt](https://labspc.gitbook.io/cnippets/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://labspc.gitbook.io/cnippets/chap5.-zhi-zhen-yu-shu-zu/5.2-zhi-zhen-yu-han-shu-can-shu.md).

# 5.2 指针与函数参数

在 4.1 节中有有个交换两变量值的例子：

```c
// 交换两个变量的值

#include <stdio.h>

// 引入中间变量e，用于交换变量c d 的值
void Swap(int c, int d) {
    int e=0;
    e = c;
    c = d;
    d = e;
    // printf("%d %d", c, d);
}

int main(void) {
    int a = 0, b = 0;
    scanf("%d %d", &a, &b);
    printf("a=%d b=%d\n", a, b); //交换前测试

    Swap(a, b);
    printf("a=%d b=%d\n", a, b);

    return 0;

}
```

这是 SOL 1 方法，为什么没有实现呢？c 语言是传值调用，这里并没有改变值，要传指针才行。

指针作为函数参数：

```c
#include <stdio.h>
//指针作为函数参数
void decompose(double x, long *int_part, double *frac_part)
{
    *int_part = (long)x;
    *frac_part = x - *int_part;
}

    double x = 3.14159265358979323846;
    long int_part;
    double frac_part;

int main()
{
    // 调用函数
    decompose(x, &int_part, &frac_part);
    // 输出结果
    printf("整数部分: %ld\n", int_part);
    printf("小数部分: %f\n", frac_part);

    return 0;
}
```
