C 不支持通过引用传递变量.怎么做
C does not support passing a variable by reference. How to do it?
这是C++代码:
void Foo(char* k, struct_t* &Root)
如何在纯C中实现它?
你是对的,C 不支持通过引用传递(因为它是由 C++ 定义的)。但是,C 支持传递指针。
从根本上说,指针是引用。指针是存储变量可以定位的内存地址的变量。因此,标准指针C++引用相当。
所以在您的情况下,void Foo(char *k, struct_t* &Root)
类似于 void Foo(char *k, struct_t **Root)
.要访问Foo
函数中的Root
结构,您可以这样说:
void Foo(char *k, struct_t **Root){
// Retrieve a local copy of the 1st pointer level
struct_t *ptrRoot = *Root;
// Now we can access the variables like normal
// Perhaps the root structure contains an integer variable:
int intVariable = ptrRoot->SomeIntegerVariable;
int modRootVariable = doSomeCalculation(intVariable);
// Perhaps we want to reassign it then:
ptrRoot->SomeIntegerVariable = modRootVariable;
}
因此,仅传递指针等效于传递引用。
相关文章:
- 为什么istream不支持右值提取
- vs 2015 constexpr变量不恒定,但与2019相比还好吗
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 错误:(-210:不支持的格式或格式组合)功能'create'中的硬件视频解码器不支持视频源
- 在头文件中定义变量不会出错
- Casablanca/cpprestsdk listener.support接受函数,但不支持方法
- 为什么C++不支持对未初始化变量进行智能分析?
- 在 c++ 中连接字符串和整数,以便在 C++ 11 不支持计算机的情况下读取多个文件
- 为什么我的数组值与此处的全局变量不匹配?
- 在 Ubuntu 18.04 上编译 GLSL 着色器时"不支持 GLSL 3.30"?
- 静态成员变量不会由 gettext 转换
- 为什么联机C++ IDE 不支持"graphics.h"头文件?
- Clang编译器不支持aarch64-apple-darwin上的-fxray-instrument
- 为什么我的 clang 格式不支持 PPDirectiveIndentStyle 选项?
- 是否允许类类型的 std::function 成员变量(不完整类型)?
- 为什么某些 STL 容器(堆栈、队列、优先级队列)不支持迭代器?
- 变量不在 lambda 的范围内
- 为什么不支持 Xcode 1.5?
- 为什么向量不支持 size 作为成员变量,就像 Java 类中的长度变量一样,而是函数 size()?
- C 不支持通过引用传递变量.怎么做