Thread: Passing Through A Variable Argument List

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    Question Passing Through A Variable Argument List

    Hello,

    I'm just trying to define a small API for this wickedy thing I'm doing. While some functions are complex, others merely wrap certain C runtime functions.

    Right now I'm trying to define a function that wraps sscanf. As this function uses a variable argument list, assuming I declare the wrapper with the same arguments how do I pass it a va_arg?
    Code:
    int String_Scan(char *buffer, char *format, ...)
    {
       // ?
       // Profit
    }

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    You might want to have a look at this thread, especially post #4.

  3. #3
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    I don't think I could use a for loop and make individual calls to sscanf because I would also have to look through the format string and make sure it points to the nearest specifier. Seems to over-complicate things somewhat.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I think you have to. You can just pass over "...". I think you have to do the conversion yourself.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273
    I suppose rather than fluffing around I could just use:-
    Code:
    #define String_Scan   sscanf

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if String_Scan ultimately calls sscanf, then you need to use the va_arg stuff and pass the "..." onto vsscanf()

    In fact, it's generally a lot easier all round if you begin by providing vString_Scan() where the ... is replaced by a va_arg parameter, then String_Scan() is just a very thin wrapper around vString_Scan() which does all the work (and calls vsscanf).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by SMurf View Post
    Right now I'm trying to define a function that wraps sscanf. As this function uses a variable argument list, assuming I declare the wrapper with the same arguments how do I pass it a va_arg?
    vsscanf() is meant for this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. intercepting functions with a variable argument list
    By Peter_Machner in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2005, 06:25 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM