论证和功能 [英] Arguments and Functions

查看:93
本文介绍了论证和功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的小组,


我正在尝试修改全局

照明光线跟踪器RADIANCE的部分源代码。具体来说,我试图实现一个不同的光线/三角交叉算法。我的C

编程技巧最好是外行,但我已经成功实现了这个算法的三角形多边形(源代码是我想要的b $ b)以前修改是针对三角形网格。关于将函数传递给函数,我有一个

的问题。对于我已经读过并经历过的所有事情,如果有一个函数需要两个

参数,那么这些参数必须与函数调用一起(即

函数(arg1,arg2))和参数类型必须与

声明函数中的类型匹配。在我正在修改的源代码中,我来了一个实例,其中一个被调用的函数,即
需要两个参数,被调用而没有任何参数。在

后面的文件中:

http://www.radiance-online.org/cgi-b...c/rt/o_mesh.c?
第167行的
,它出现了对我来说,函数mesh_hit被调用了
,但是没有传递给它的参数。函数mesh_hit

需要两个参数(oset,r)。这段代码确实成功执行了

,但我不知道它是如何工作的。我搜索了所有的书籍,我搜索了网络和usenet但我找不到任何

的答案。任何人都可以向我解释这个吗?


BTW


对于任何想要解决这个问题的人,这里是
的地址
下载ray.h(对于RAY类型):

http://www.radiance-online.org/cgi-b.../src/rt/ray.h?


和object.h(用于OBJECT类型):

http://www.radiance-online.org/cgi-b...ype = text / plain


谢谢


马库斯

解决方案

Marc说:


< snip>

http://www.radiance-online.org/cgi-b...c/rt/o_mesh.c?

,它出现了对我来说,函数mesh_hit被调用但没有参数被传递给它。




你的意思是这个吗?


rcont.hitf = mesh_hit;


如果是,那么不,这不是函数调用。它是一个

函数指针值赋值给函数指针对象。

在ray.h中,你会找到以下结构成员:


void(* hitf)(OBJECT *,struct ray *);


这是一个指向带两个参数的函数的指针(类型OBJECT *

和struct ray *)并且不返回值。


rcont.hitf = mesh_hit;意味着你在mesh_hit函数中指向rcont

的hitf成员。这不涉及对该函数的调用。

然而,它所暗示的是,稍后您可以通过指针调用函数

,而不是通过它实际名称。

的语法如下:


(* rcont.hitf)(& myobj,& myray);
<但是,b $ b虽然反常地说:


rcont.hitf(& myobj,& myray);


也会合法。确切的语法(&符号,对象名称等)

显然取决于相关对象的类型和名称。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


Richard Heathfield< in **** *@invalid.invalid>写道:

rcont.hitf = mesh_hit;意味着你将指向rcont
的hitf成员指向mesh_hit函数。这不涉及对该函数的调用。
然而,它所暗示的是,稍后您可以通过指针调用函数
,而不是通过其实际名称。该
的语法类似于:

(* rcont.hitf)(& myobj,& myray);

虽然,反常:

rcont.hitf(& myobj,& myray);

也是合法的。确切的语法(&符号,对象名称等)
显然取决于相关对象的类型和名称。




考虑使用的verbage根据标准,它是第一个

我会看到更多的变态。函数调用是/ always / done

通过指向函数的指针;它只是函数指示符是

隐式转换为指向函数的指针。


第一次调用有点奇怪,因为从观点来看

标准,它是一个指向函数的指针,凭借你的

解引用,转换为函数类型,随后,因为它

不是&的操作数。或者sizeof运算符,在被调用之前转换回指向函数的指针。


:-)

< Micah Cowan说:

Richard Heathfield< in ***** @ invalid.invalid>写道:

其语法如下:

(* rcont.hitf)(& myobj,& myray) ;

虽然,反过来说:

rcont.hitf(& myobj,& myray);

也是合法的。 [...]



考虑到标准使用的verbage,它是第一个我将更多地视为变态的。




< shrug>我们可以同意不同吗?我发现*最有帮助的是

在第一次阅读时解密代码。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


Dear Group,

I am attempting to modify a portion of the source code for the global
illumination raytracer, RADIANCE. Specifically, I am trying to
implement a different ray/triangle intersection algorithm. My C
programming skills are laymen at best but I have successfully
implemented this algorithm for triangle polygons (the source code that
I am trying to modify is for a triangular mesh) previously. I have a
question concerning passing arguments on to functions. For all that I
have read and experienced, if there is a function that requires two
arguments, then those arguments must be with the function call (i.e.
function(arg1,arg2)) and the argument types must match the types in the
declared function. In the source code that I am modifying, I have come
across an instance wherein a function that is being called, which
requires two arguments, is being called without any arguments. In the
following file:

http://www.radiance-online.org/cgi-b...c/rt/o_mesh.c?
on line 167, it appears to me that the function mesh_hit is being
called but no arguments are being passed to it. The function mesh_hit
requires two arguments (oset, r). This code does successfully execute
but I do not know how it works. I have searched through all of my C
books and I have searched the web and usenet but I can not find any
answers. Can anyone explain this to me?

BTW

For anyone who wishes to tackle this issues, here is the address to
download ray.h (for the RAY type):

http://www.radiance-online.org/cgi-b.../src/rt/ray.h?

and object.h (for the OBJECT type):

http://www.radiance-online.org/cgi-b...ype=text/plain

Thanks

Marcus

解决方案

Marc said:

<snip>

http://www.radiance-online.org/cgi-b...c/rt/o_mesh.c?

on line 167, it appears to me that the function mesh_hit is being
called but no arguments are being passed to it.



You mean this?

rcont.hitf = mesh_hit;

If so, then no, this is not a function call. It is an assignment of a
function pointer value to a function pointer object.

In ray.h, you''ll find the following structure member:

void (*hitf)(OBJECT *, struct ray *);

This is a pointer to a function that takes two parameters (of types OBJECT *
and struct ray *) and returns no value.

rcont.hitf = mesh_hit; means that you''re pointing the hitf member of rcont
at the mesh_hit function. This does not involve a call to that function.
What it does imply, however, is that later on you can call the function
through the pointer, rather than by its actual name. The syntax for that
would be something like:

(*rcont.hitf)(&myobj, &myray);

although, perversely:

rcont.hitf(&myobj, &myray);

would also be legal. The exact syntax (ampersands, object names, and so on)
obviously depends on the types and names of the objects in question.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


Richard Heathfield <in*****@invalid.invalid> writes:

rcont.hitf = mesh_hit; means that you''re pointing the hitf member of rcont
at the mesh_hit function. This does not involve a call to that function.
What it does imply, however, is that later on you can call the function
through the pointer, rather than by its actual name. The syntax for that
would be something like:

(*rcont.hitf)(&myobj, &myray);

although, perversely:

rcont.hitf(&myobj, &myray);

would also be legal. The exact syntax (ampersands, object names, and so on)
obviously depends on the types and names of the objects in question.



Considering the verbage used by the standard, it''s the first one that
I would see more as a perversion. A function call is /always/ done
through a pointer-to-function; it''s just that function designators are
implicitly converted to pointer-to-function.

The first invocation is a bit strange because, from the viewpoint of
the Standard, it''s a pointer-to-function that, by virtue of your
dereference, is converted to function type and subsequently, since it
is not the operand of the & or sizeof operators, gets converted back
into a pointer-to-function before being called.

:-)


Micah Cowan said:

Richard Heathfield <in*****@invalid.invalid> writes:

The syntax
for that would be something like:

(*rcont.hitf)(&myobj, &myray);

although, perversely:

rcont.hitf(&myobj, &myray);

would also be legal. [...]



Considering the verbage used by the standard, it''s the first one that
I would see more as a perversion.



<shrug> Can we agree to differ? I find that the * is most helpful in
deciphering the code on a first reading.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


这篇关于论证和功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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