Results 1 to 2 of 2

Thread: va_start, va_arg, and va_end macros

  1. #1
    amac
    Guest

    Question va_start, va_arg, and va_end macros

    I was wondering if someone that has used these and gotten them to work would help me out...

    I found an example which works... but when I try to do it... it doesnt... I almost type exactly the same thing in the example and still nothing...

    Code:
    #include <stdio.h>
    #include <stdarg.h>
    
    int count ( int first, ... );
    
    void main (void)
    {
        printf ("%d", count ( 1, 2, 3, 4, 5, 6 ) );
    }
    
    int count ( int first, ... )
    {
    
        int count = 1, i = 0;
        va_list start;
    
        va_start ( start, first );
    
        while (1)
        {
    
             i = va_arg (start, int);
             
             if (i != -1) count++;
             else break;
    
        }
    
        va_end ( start );
    
        return count;
    
    }
    This is how the example does it... by lookin at it... this should work... but doesnt.

    Can anyone help?

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    you have no indication how many parameters are passed.
    The (i != -1) can be used in the documentation example because every argument list has -1 as the last argument.
    printf knows how many args to expect because of the formatstring
    your function simply continues to retrieve "arguments" until it eventually reaches locked memory, causing an access violation.
    if you call it like this: count(1, 2, 3, 4, 5, 6, 7, 8, 9, -1) it will work and return 9

    all the buzzt
    CornedBee

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