Results 1 to 10 of 10

Thread: Variable argument functions

  1. #1
    ChimpFace9000
    Guest

    Post 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, ...);
    }

  2. #2
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    I don't think there is any way other passing them manually 1 by 1.
    Baaaaaaaaah

  3. #3
    ChimpFace9000
    Guest
    Well how do i do that?

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  5. #5
    ChimpFace9000
    Guest
    That doesnt answer my question at all.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  8. #8
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width