在 C 中通过引用传递 [英] Passing by reference in C

查看:27
本文介绍了在 C 中通过引用传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 C 不支持通过引用传递变量,为什么它可以工作?

If C does not support passing a variable by reference, why does this work?

#include <stdio.h>

void f(int *j) {
  (*j)++;
}

int main() {
  int i = 20;
  int *p = &i;
  f(p);
  printf("i = %d
", i);

  return 0;
}

输出:

$ gcc -std=c99 test.c
$ a.exe
i = 21 

推荐答案

因为您将指针的传递给方法,然后取消引用它以获取指向的整数.

Because you're passing the value of the pointer to the method and then dereferencing it to get the integer that is pointed to.

这篇关于在 C 中通过引用传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆