哪些类型是合法的(...)参数? [英] What types are legal as (...) arguments?

查看:85
本文介绍了哪些类型是合法的(...)参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


我正在制作一个程序,我正在大量使用(...)参数

我对他们有疑问。这一切都运行得很好,但是

我得到一个编译器的警告,这让我觉得

也许我的用法不便携,我想知道C对我的用例说什么(而不是我的编译器说的话)。


考虑:
< br $>
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 (...) arguments
and i have a question about them. It''s all working fine and well, but
i''m getting a warning from one compiler which makes the think that
perhaps my usage isn''t portable, and i want to find out what C says
about 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 objects
other than built-in types here? gcc and SunStudio compilers don''t
complain about it and the code words as expected, but tcc says
"warning: assignment of read-only location" (but it still works as
expected), so my suspicion is that gcc and SunCC are being too lenient
on me and that tcc is trying to tip me off to a violation of the
standard.

If it makes a difference, i''m trying to code for pre-C99, but i also
don''t mind if i have to rely on C99 features.

To cut off the anticipated counter-question in advance: "why not pass
pointers to my_struct instead?" The answer is:

a) i do. i have two variants of my routines: one takes a list of
pointers and one takes a list of value types.
b) both approaches have slightly different implications in my library
and 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''t
shown us the declaration of my Context. Is it an object of type
struct 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 <ks...@mib.orgwrote:

如果您发布了更完整的代码,将会很有帮助。你还没有向我们展示我的上下文的声明。它是一个类型的对象

struct context *?
It would be helpful if you posted more complete code. You haven''t
shown us the declaration of my Context. Is it an object of type
struct 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 to
pass to va_start(). The exact types used are not significant for this
example, either, other than to demonstrate that i''m passing structs of
arbitrary size *by value* via (...)/va_list, and i''m curious as to
whether doing so is strictly C (or C99) compliant or whether i''m
unintentionally relying on a compiler-specific extension.


Stephan Beal写道:
Stephan Beal wrote:

Hello,all!


我正在开发一个程序,我正在大量使用(...)参数

我对它们有疑问。这一切都运行得很好,但是

我得到一个编译器的警告,这让我觉得

也许我的用法不便携,我想知道C对我的用例说什么(而不是我的编译器说的话)。


考虑:
< br $>
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 (...) arguments
and i have a question about them. It''s all working fine and well, but
i''m getting a warning from one compiler which makes the think that
perhaps my usage isn''t portable, and i want to find out what C says
about 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 "struct
my_struct", not just plain "my_struct". Did you make a typedef you
haven''t shown us, or are you''re compiling this as C++ code, rather
than as C code? If this is C++, your question actually belongs on
comp.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 objects
other than built-in types here? gcc and SunStudio compilers don''t
complain about it and the code words as expected, but tcc says
"warning: assignment of read-only location" (but it still works as
expected), so my suspicion is that gcc and SunCC are being too lenient
on me and that tcc is trying to tip me off to a violation of the
standard.



您编写的代码似乎不包含任何分配

只读位置。您向我们展示的唯一可能是存储在只读内存中的内容是''结束',并且您的代码读取了该值,

它没有写不出来。


因此,我怀疑问题可能是由于某些代码你没有向我们展示。请尽可能简单地提供完整的,可编辑的代码,

。请确认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 of
read-only location". The only thing you''ve shown us that could be
stored 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 you
haven''t shown us. Please provide a complete, compilable piece of code,
as simple as possible. Please confirm that tcc produces the same error
message 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 any
warning 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);
}


这篇关于哪些类型是合法的(...)参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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