通过参考用C传递 [英] Passing by reference in C

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

问题描述

如果C不支持通过引用传递一个变量,为什么这项工作?

 的#include<&stdio.h中GT;无效F(INT * j)条{
  (* j)条++;
}诠释主(){
  INT I = 20;
  为int * p =&放;我;
  F(p)的;
  输出(I =%d个\\ N,I);  返回0;
}

输出


$ GCC -std = C99 test.c的
$ A.EXE
我= 21


解决方案

由于你传递的的指针方法,然后取消引用它来获得所指向的整数。

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\n", i);

  return 0;
}

Output

$ 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天全站免登陆