本文介绍了哪些类型是合法的(...)参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好! 我正在制作一个程序,我正在大量使用(...)参数 我对他们有疑问。这一切都运行得很好,但是 我得到一个编译器的警告,这让我觉得 也许我的用法不便携,我想知道C对我的用例说什么(而不是我的编译器说的话)。 考虑: struct my_struct { int i; struct my_struct * left; struct some_struct bar; }; 现在,假设我有一个例程: void do_mything(struct context *,...); 我称之为: static const my_struct end; / *信号列表结束的特殊参数 * / my_struct foo; my_struct bar; 。 ...初始化结构... do_mything(myContext,foo,bar,end); 我的问题很简单:是吗合法(严格来说)传递对象 除了内置类型吗? gcc和SunStudio编译器没有按照预期的那样抱怨它和代码字,但tcc说 警告:分配只读位置 (但它仍然可以作为预期的b $ b),所以我怀疑gcc和SunCC对我来说太宽容了,而且tcc正试图让我知道违反了 标准。 如果它有所作为,我正在尝试为C99之前编码,但我也是 不介意我是否必须依赖C99功能。 提前切断预期的反问题:为什么不通过 指向my_struct的指针?答案是: a)我这样做。我有两个我的例程变体:一个带有一个 指针列表,一个带有一个值类型列表。 b)这两种方法在我的库中有不同的含义 在两种情况下都很有用。 : - ?Hello, all!i''m working on a program where i''m making heavy use of (...) argumentsand i have a question about them. It''s all working fine and well, buti''m getting a warning from one compiler which makes the think thatperhaps my usage isn''t portable, and i want to find out what C saysabout my use case (rather than what my compilers say).Consider:struct my_struct {int i;struct my_struct * left;struct some_struct bar;};Now, assuming i have a routine:void do_mything( struct context *, ... );i''m calling that like:static const my_struct end; /* special argument to signal end of list*/my_struct foo;my_struct bar;.... initialize structs ...do_mything( myContext, foo, bar, end );My question is simple: is it legal (strictly speaking) to pass objectsother than built-in types here? gcc and SunStudio compilers don''tcomplain about it and the code words as expected, but tcc says"warning: assignment of read-only location" (but it still works asexpected), so my suspicion is that gcc and SunCC are being too lenienton me and that tcc is trying to tip me off to a violation of thestandard.If it makes a difference, i''m trying to code for pre-C99, but i alsodon''t mind if i have to rely on C99 features.To cut off the anticipated counter-question in advance: "why not passpointers to my_struct instead?" The answer is:a) i do. i have two variants of my routines: one takes a list ofpointers and one takes a list of value types.b) both approaches have slightly different implications in my libraryand both are useful in different contexts.:-?推荐答案 Stephan Beal< sg **** @ googlemail.comwrites: [...]Stephan Beal <sg****@googlemail.comwrites:[...] 考虑: struct my_struct { int i; struct my_struct * left; struct some_struct bar; }; 现在,假设我有一个例程: void do_mything(struct context *,...) ; 我称之为: static const my_struct end; / *信号列表结束的特殊参数 * / my_struct foo; my_struct bar; 。 ..初始化结构... do_mything(myContext,foo,bar,end);Consider:struct my_struct { int i; struct my_struct * left; struct some_struct bar;};Now, assuming i have a routine: void do_mything( struct context *, ... );i''m calling that like:static const my_struct end; /* special argument to signal end of list*/my_struct foo;my_struct bar;... initialize structs ...do_mything( myContext, foo, bar, end ); [...] 如果您发布了更完整的代码,将会很有帮助。你还没有向我们展示我的上下文的声明。它是一个类型的对象 struct context *? - Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst> Nokia 我们必须做点什么。这是事情。因此,我们必须这样做。 - Antony Jay和Jonathan Lynn,是部长[...]It would be helpful if you posted more complete code. You haven''tshown us the declaration of my Context. Is it an object of typestruct context*?--Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>Nokia"We must do something. This is something. Therefore, we must do this."-- Antony Jay and Jonathan Lynn, "Yes Minister" 10月24日,6日:41 pm,Keith Thompson< ks ... @ mib.orgwrote:On Oct 24, 6:41 pm, Keith Thompson <[email protected]: 如果您发布了更完整的代码,将会很有帮助。你还没有向我们展示我的上下文的声明。它是一个类型的对象 struct context *?It would be helpful if you posted more complete code. You haven''tshown us the declaration of my Context. Is it an object of typestruct context*? 这与此目的无关 - 我对(...)args感兴趣。 在这个例子中上下文只有那里我有一个参数 传递给va_start()。使用的确切类型对于这个 示例来说并不重要,除了证明我通过(*)传递 任意大小*的结构*。 ..)/ va_list,我很好奇 这样做是否严格遵守C(或C99)或者我是否 无意中依赖于特定于编译器的扩展。That''s irrelevant for this purpose - i''m interested in the (...) args.In this example the context is only there so i have an argument topass to va_start(). The exact types used are not significant for thisexample, either, other than to demonstrate that i''m passing structs ofarbitrary size *by value* via (...)/va_list, and i''m curious as towhether doing so is strictly C (or C99) compliant or whether i''munintentionally relying on a compiler-specific extension. Stephan Beal写道:Stephan Beal wrote: Hello,all! 我正在开发一个程序,我正在大量使用(...)参数 我对它们有疑问。这一切都运行得很好,但是 我得到一个编译器的警告,这让我觉得 也许我的用法不便携,我想知道C对我的用例说什么(而不是我的编译器说的话)。 考虑: struct my_struct { int i; struct my_struct * left; struct some_struct bar; }; 现在,假设我有一个例程: void do_mything(struct context *,...); 我称之为: static const my_struct end; / *信号列表结束的特殊参数 * / my_struct foo; my_struct bar;Hello, all!i''m working on a program where i''m making heavy use of (...) argumentsand i have a question about them. It''s all working fine and well, buti''m getting a warning from one compiler which makes the think thatperhaps my usage isn''t portable, and i want to find out what C saysabout my use case (rather than what my compilers say).Consider:struct my_struct { int i; struct my_struct * left; struct some_struct bar;};Now, assuming i have a routine: void do_mything( struct context *, ... );i''m calling that like:static const my_struct end; /* special argument to signal end of list*/my_struct foo;my_struct bar; 在上述每种情况下,你应该有struct my_struct,而不仅仅是普通的my_struct。你做了一个typedef你 没有给我们看,或者你是在编译这个C ++代码,而不是b $ b比C代码?如果这是C ++,你的问题实际上属于 comp.lang.c ++,而不是这里。In each of the above cases, you were supposed to have "structmy_struct", not just plain "my_struct". Did you make a typedef youhaven''t shown us, or are you''re compiling this as C++ code, ratherthan as C code? If this is C++, your question actually belongs oncomp.lang.c++, not here. ...初始化结构.. 。 do_mything(myContext,foo,bar,end); 我的问题很简单:是否合法(严格来说)通过对象 除了内置类型了吗? gcc和SunStudio编译器没有按照预期的那样抱怨它和代码字,但tcc说 警告:分配只读位置 (但它仍然可以作为预期的b $ b),所以我怀疑gcc和SunCC对我来说太宽容了,而且tcc正试图让我知道违反了 标准。... initialize structs ...do_mything( myContext, foo, bar, end );My question is simple: is it legal (strictly speaking) to pass objectsother than built-in types here? gcc and SunStudio compilers don''tcomplain about it and the code words as expected, but tcc says"warning: assignment of read-only location" (but it still works asexpected), so my suspicion is that gcc and SunCC are being too lenienton me and that tcc is trying to tip me off to a violation of thestandard. 您编写的代码似乎不包含任何分配 只读位置。您向我们展示的唯一可能是存储在只读内存中的内容是''结束',并且您的代码读取了该值, 它没有写不出来。 因此,我怀疑问题可能是由于某些代码你没有向我们展示。请尽可能简单地提供完整的,可编辑的代码, 。请确认tcc在简化代码上产生与实际代码相同的错误 消息。 以下是最简单的代码我可以提出, 与你已经写好的一致。 tcc在编译时会发出任何 警告消息吗? #include< stdarg.h> struct context { int i; }; struct some_struct { int i; }; struct my_struct { int i; struct my_struct * left; struct some_struct bar; }; void do_mything(struct context *,...); int main(void) { static const struct my_struct end; / *信号列表结尾的特殊参数* / struct my_struct foo = {0}; struct my_struct bar = {0}; struct context actual = {0 }; struct context * myContext =& actual; do_mything(myContext,foo,bar,end); 返回0; } void do_mything(struct context * myContext,...){ va_list ap; va_start(ap,myContext); str uct my_struct foo = va_arg(ap,struct my_struct); struct my_struct bar = va_arg(ap,struct my_struct); struct my_struct end = va_arg(ap,struct my_struct ); va_end(ap); }The code you''ve written does not seem to contain any "assignment ofread-only location". The only thing you''ve shown us that could bestored in read-only memory is ''end'', and your code reads that value,it doesn''t write it.Therefore, I suspect that the problem may be due to some code that youhaven''t shown us. Please provide a complete, compilable piece of code,as simple as possible. Please confirm that tcc produces the same errormessage on the simplified code as it does on your actual code.The following is the simplest piece of code I could come up with,consistent with what you''ve already written. Does tcc issue anywarning messages when compiling it?#include <stdarg.h>struct context {int i;};struct some_struct {int i;};struct my_struct {int i;struct my_struct * left;struct some_struct bar;};void do_mything( struct context *, ... );int main(void){static const struct my_struct end;/* special argument to signal end of list */struct my_struct foo = {0};struct my_struct bar = {0};struct context actual = {0};struct context *myContext = &actual;do_mything( myContext, foo, bar, end );return 0;}void do_mything( struct context *myContext, ... ){va_list ap;va_start(ap, myContext);struct my_struct foo = va_arg(ap, struct my_struct);struct my_struct bar = va_arg(ap, struct my_struct);struct my_struct end = va_arg(ap, struct my_struct);va_end(ap);} 这篇关于哪些类型是合法的(...)参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 17:10