C中的参考文献

aks*_*aks 1 c c++ reference

可能重复:
在C下通过引用传递指针参数?

参考变量是C++概念吗?它们是否可用于C?如果在C中可用,为什么我的代码会出现编译错误?

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

int main()
{
  int a = 10;
  int b = 20;
  int &c = a;
  int &d = b;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

bash-3.2$ gcc test.c
test.c: In function `main':
test.c:12: error: parse error before '&' token
Run Code Online (Sandbox Code Playgroud)

为什么?

Mat*_*vis 11

C没有引用作为类型.C中的'&'仅用于获取变量的地址.它不能以您尝试在C中使用它的方式使用.


Bru*_*eis 6

  1. 它是C++上的一个概念(但不是唯一的)
  2. 不,它不适用于标准C.
  3. 见1和2.见*1和*2.

  • 点3应该是"看*1和*2"吗?;) (3认同)

Ale*_*lex 6

你不能用它来声明变量:

int &c = a;
Run Code Online (Sandbox Code Playgroud)

operator&用于获取变量的内存地址.所以,你可以写例如:

int a = 10;
int *c;
c = &a;
Run Code Online (Sandbox Code Playgroud)