Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
735 views
in Technique[技术] by (71.8m points)

c - Pass va_list or pointer to va_list?

Suppose I have a function which takes variadic arguments (...) or a va_list passed from another such function. The main logic is in this function itself (let's call it f1), but I want to have it pass the va_list to another function (let's call it f2) which will determine the next argument type, obtain it using va_arg, and properly convert and store it for the caller to use.

Is it sufficient to pass a va_list to f2, or is it necessary to pass a pointer to va_list. Unless va_list is required to be an array type or else store its position data at the location the va_list object points to (rather than in the object itself), I can't see how passing it by value could allow the calling function (f1) to 'see' the changes the called function made by va_arg.

Can anyone shed light on this? I'm interested in what the standard requires, not what some particular implementation allows.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like you'll need to pass a pointer to the va_list. For more info, see the C99 standard document section 7.15.In particular, bullet point 3 states:

The object ap may be passed as an argument to another function; if that function invokes the va_arg macro with parameter ap, the value of ap in the calling function is indeterminate and shall be passed to the va_end macro prior to any further reference to ap

[my italics]

Edit: Just noticed a footnote in the standard:

215) It is permitted to create a pointer to a va_list and pass that pointer to another function, in which case the original function may make further use of the original list after the other function returns

So you can pass a pointer to the va_list and do va_arg(*va_list_pointer) in the called function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...