如何通过各种结构来发挥作用? [英] How to pass various structures to function?

查看:58
本文介绍了如何通过各种结构来发挥作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建一个将任何

结构作为参数的函数?例如,

如果我想要根据具体情况将名为astruct或bstruct或cstruct的

结构传递给

功能?有点像这样:

myfunc(char * str,struct astruct * as)

从谷歌搜索,它总是出现一个特定的

结构被传递给函数。


-TIA

Is it possible to create a function which will take any
number of structures as an argument? For example, what
if I wanted depending on the circumstances to pass a
structure named astruct, or bstruct, or cstruct, to
the function? Sort of like this:
myfunc(char * str, struct astruct *as)
From googling, it always appears that one specific
structure is passed to the function.

-TIA

推荐答案

Fred写道:
Fred wrote:

是否可以创建一个函数,它将任何

个结构作为参数?例如,

如果我想要根据具体情况将名为astruct或bstruct或cstruct的

结构传递给

功能?有点像这样:


myfunc(char * str,struct astruct * as)


从谷歌搜索,它总是出现一个特定的

结构传递给函数。
Is it possible to create a function which will take any
number of structures as an argument? For example, what
if I wanted depending on the circumstances to pass a
structure named astruct, or bstruct, or cstruct, to
the function? Sort of like this:
myfunc(char * str, struct astruct *as)
From googling, it always appears that one specific
structure is passed to the function.



使用< stdarg.h>的设施。请记住,您需要至少一个

命名参数,然后才会出现变量参数列表。在您的

情况下,名称不佳的char * str可用于传递有关参数数量和类型的信息



printf()和scanf()系列。你的例子使用指针结构

,如果你混合使用不同定义的struct

参数以及改变它们的数量,这是个好主意。

参数列表,其参数是从基本的
整数和浮动中提取的在转向更复杂的参数之前的类型

和参数列表。

Use the facilities of <stdarg.h>. Remember that you need at least one
named argument after which the variable argument list occurs. In your
case the poorly-named char *str could be used to pass information about
the number and types of arguments, as does the format string for the
printf() and scanf() families. Your example uses pointers-to-structs
which is a good idea if you are mixing differently defined struct
arguments as well as changing their number.

I would, however, suggest that you start by learning how to use variable
argument lists with the arguments being drawn from the fundamental
integer and floating types before moving on to more complex arguments
and argument lists.


在文章< rv **********中********************@comcast.com>,

Fred< it **** @ cdw.comwrote:
In article <rv******************************@comcast.com>,
Fred <it****@cdw.comwrote:

>是否可以创建一个将任意数量的结构作为参数的函数?例如,如果我想根据具体情况通过一个名为astruct的结构,或者bstruct,或cstruct,那么
该函数是什么?
>Is it possible to create a function which will take any
number of structures as an argument? For example, what
if I wanted depending on the circumstances to pass a
structure named astruct, or bstruct, or cstruct, to
the function?



你真的想做什么?


我在遇到一个正确的案例时遇到了麻烦答案是

除了为每个结构写一个不同的函数之外的其他东西,

可能在为所有
选项"。

dave


-

Dave Vandervies dj ****** @ csclub.uwaterloo.ca

有一个名叫Ross St John的人,每个人都是希望会不见了。

他称自己为Cass,我们一团糟地打他,

然后放出一个clc打哈欠。 --Dann Corbit in comp.lang.c

What are you really trying to do?

I''m having trouble coming up with a case where the Right Answer is
something other than "write a different function for each struct",
possibly in the degenerate case of "use a single struct for all of
the options".
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
There was a twit named Ross St John, who everyone wished would be gone.
He called himself Cass, We plonked him en-masse,
And let out a c-l-c yawn. --Dann Corbit in comp.lang.c


On Thu,2007年5月3日03:11:04 +0000,Dave Vandervies写道:
On Thu, 03 May 2007 03:11:04 +0000, Dave Vandervies wrote:

你真的想做什么?


我在遇到正确答案的案例时遇到问题

除了为每个结构写一个不同的函数之外的东西,

可能在使用单个结构的所有

选项的退化情况下;。


dave
What are you really trying to do?

I''m having trouble coming up with a case where the Right Answer is
something other than "write a different function for each struct",
possibly in the degenerate case of "use a single struct for all of
the options".
dave




我正在尝试搜索结构中的数据,并且想要有一个

函数,我可以传递多个结构。



I''m trying to search for data in a structure, and want to have 1
function that I can pass multiple structures to.


这篇关于如何通过各种结构来发挥作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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