在C基准传 [英] Pass by reference in C

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

问题描述

我想通过C基准使用的通过,使得该函数可以修改传递给它的参数值。这是函数签名:

I'm trying to use pass by reference in C so that the function can modify the values of the parameters passed to it. This is the function signature:

int locate(char *name, int &s, int &i)

然而,当我尝试编译它,我得到这个错误具体是指上述行:

However when I try to compile it I get this error that refers specifically to the above line:

错误:预期',',','或')'之前
  '和;'令牌

error: expected ‘;’, ‘,’ or ‘)’ before '&' token

如果我删除'和;'该程序将编译,但它不能正常运行,效果显着。什么是错在这里?我怎么可以参照工作拨打电话?

If I remove the '&' the program will compile, but it will not function correctly, obviously. What's wrong here? How can I make call by reference work?

推荐答案

C没有引用。您需要将指针传递给变量要修改:

C does not have references. You need to pass a pointer to the variable you wish to modify:

int locate(char *name, int *s, int *i)
{
    /* ... */

    *s = 123;
    *i = 456;
}

int s = 0;
int i = 0;
locate("GMan", &s, &i);

/* s & i have been modified */

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

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