|
-
May 7th, 2002, 05:51 PM
#1
Variable argument functions
I wrote a variable argument function(va function) that requires at least one string. Then i wrote another va function that requires at least one string. How do i pass all the arguments that get passed to the second va function in a call to the first.
like...
Code:
void one(cont char* fmt, ...)
{
}
void two(const char* fmt, ...)
{
one(fmt, ...);
}
-
May 7th, 2002, 06:25 PM
#2
PowerPoster
I don't think there is any way other passing them manually 1 by 1.
-
May 7th, 2002, 07:53 PM
#3
-
May 8th, 2002, 01:19 PM
#4
Monday Morning Lunatic
Code:
#include <iostream>
#include <cstdarg>
using namespace std;
void two(int count, va_list va) {
for(int i = 0; i < count; ++i) {
cout << va_arg(va, int) << endl;
}
}
void one(int count, ...) {
va_list args;
va_start(args, count);
two(count, args);
}
VC++7, Win2000.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 8th, 2002, 02:13 PM
#5
That doesnt answer my question at all.
-
May 8th, 2002, 02:23 PM
#6
Monday Morning Lunatic
Ummm....you wanted to pass all the provided arguments?
The va_list syntax is an alternative method of passing a variable number of arguments.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 8th, 2002, 02:27 PM
#7
Monday Morning Lunatic
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 8th, 2002, 02:37 PM
#8
Frenzied Member
Originally posted by parksie
Code:
#include <iostream>
#include <cstdarg>
using namespace std;
void two(int count, va_list va) {
for(int i = 0; i < count; ++i) {
cout << va_arg(va, int) << endl;
}
}
void one(int count, ...) {
va_list args;
va_start(args, count);
two(count, args);
}
VC++7, Win2000.
Don't you need a va_end() ?
Harry.
"From one thing, know ten thousand things."
-
May 8th, 2002, 02:42 PM
#9
Monday Morning Lunatic
Probably. Apparently all it does is reset the pointer to NULL, so I don't think it's that critical on x86 systems.
That said, even though it compiled and ran perfectly on mine, it should have been in there.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 10th, 2002, 05:50 AM
#10
But, parksie, this way you can't call two with variable arguments anymore. The compiler won't allow it.
This should work:
Code:
#include <iostream>
#include <cstdarg>
using namespace std;
void two_inner(int count, va_list va) {
for(int i = 0; i < count; ++i) {
cout << va_arg(va, int) << endl;
}
}
void one(int count, ...) {
va_list args;
va_start(args, count);
two_inner(count, args);
}
void two(int count, ...) {
va_list args;
va_start(args, count);
two_inner(count, args);
}
You can take a look at all the *printf CRT functions and _output (if you have the source) and note the similarities to parksie's and my code.
BTW the source of sprintf shows how to create a fake FILE that makes stream functions write to a memory buffer. You must be extremly careful using it though.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|