void function(int& a){} [英] void function(int &a) { }

查看:89
本文介绍了void function(int& a){}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++中看到了这段代码但是在尝试使用C时会导致错误:

-------------

void function( int& a){a = 5; }

-------------

这个,传入功能 a指针而不是a价值,但是内部的b b和功能我可以访问a值不是* a但是用a。


为什么这不适用于C?

I saw this code in C++ but when tried to C causes an error:
-------------
void function(int &a) { a = 5; }
-------------
with this, passed in "function" the "a" pointer instead of "a" value, BUT
inside "function" I have access to "a" value not with "*a" but with "a".

Why this is not working in C?

推荐答案

" < - Chameleon - >" < CH ****** @ hotmail.NOSPAM.com>在消息中写道

news:bg ********** @ nic.grnet.gr ...
"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:bg**********@nic.grnet.gr...
我在C ++中看到了这段代码但是在试用时到C会导致错误:
-------------
void function(int& a){a = 5; }
-------------
I saw this code in C++ but when tried to C causes an error:
-------------
void function(int &a) { a = 5; }
-------------




您试图通过引用传递,这是特定于C ++的。如果你不想复制''a''(你很难担心这个问题)你可以试试两件

的东西。 :


void function(int * a){* a = 5; }


或者只是删除&符号


我不会在这里引用,因为这个新闻组是针对C标准的,

不是C ++



You are trying to pass by reference, which is C++ specific. You can try two
things, if you don''t want to make a copy of ''a'' (which an int is hardly
anything to worry about):

void function(int *a) { *a = 5; }

or just remove the ampersand altogether

I won''t get into references here as this newsgroup is for the C standard,
not C++


- Chameleon - 写道:
- Chameleon - wrote:
我在C ++中看到了这段代码但是在试用时到C会导致错误:
-------------
void function(int& a){a = 5;与此相关的-------------
,传入功能 a指针而不是a价值,但内部功能我可以访问a值不是* a但是使用a。

为什么这不适用于C?
I saw this code in C++ but when tried to C causes an error:
-------------
void function(int &a) { a = 5; }
-------------
with this, passed in "function" the "a" pointer instead of "a" value, BUT
inside "function" I have access to "a" value not with "*a" but with "a".

Why this is not working in C?




因为C ++与C不是同一种语言因此,没有明显的原因可以解释为什么C ++功能可以在C中运行。


如果你想使用C,那么学习C.


-

Chris" electric hedgehog" Dollin

C常见问题解答: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html

C欢迎: http://www.angelfire.com/ms3/bchambl...me_to_clc.html


在< bg ********** @ nic.grnet.gr> " - 变色龙 - >" < CH ****** @ hotmail.NOSPAM.com>写道:
In <bg**********@nic.grnet.gr> "<- Chameleon ->" <ch******@hotmail.NOSPAM.com> writes:
我在C ++中看到了这段代码但是在尝试使用C时会导致错误:
-------------
void function(int& a){a = 5;与此相关的-------------
,传入功能 a指针而不是a价值,但内部功能我可以访问a值不是* a但是使用a。

为什么这不适用于C?
I saw this code in C++ but when tried to C causes an error:
-------------
void function(int &a) { a = 5; }
-------------
with this, passed in "function" the "a" pointer instead of "a" value, BUT
inside "function" I have access to "a" value not with "*a" but with "a".

Why this is not working in C?




为什么Fortran语法在C中不起作用?

为什么Pascal语法在C中不起作用?

为什么Perl语法在C中不起作用?

依此类推,等等...


下次,在发布问题之前尝试吸引你的大脑。它没有b $ b(或者,至少,不应该)伤害。


Dan

- < br $>
Dan Pop

DESY Zeuthen,RZ集团

电子邮件: Da ***** @ ifh.de


这篇关于void function(int&amp; a){}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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