I need help with the <stadarg.h> header file and va_start function

This is a discussion on I need help with the <stadarg.h> header file and va_start function within the C Programming forums, part of the General Programming Boards category; #include <stdio.h> #include <stdarg.h> double AddDouble(int x,...); // this is not a code written by me // this is an ...

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Unhappy I need help with the <stadarg.h> header file and va_start function

    #include <stdio.h>
    #include <stdarg.h>
    double AddDouble(int x,...);
    // this is not a code written by me
    // this is an example of from Sams teach yourself C in 24
    // written by Tony Zhang
    main(){
    double d1=1.5;
    double d2=2.5;
    double d3=3.5;

    printf("Given an argument: %2.1f\n",d1);
    printf("The result returned by AddDouble() is: %2.1f\n n",AddDouble (1, d1));

    printf("Given an argument: %2.1f and %2.1f \n",d1, d2);
    printf("The result returned by AddDouble() is: %2.1f\n n",AddDouble (1, d1, d2));

    printf("Given an argument: %2.1f, %2.1f , and %2.1f \n",d1, d2, d3);
    printf("The result returned by AddDouble() is: %2.1f\n n",AddDouble (1, d1, d2, d3));
    }

    double AddDouble(int x,...)
    {
    va_list arglist;
    int i;
    double result=0.0;
    printf("the number of arguments is:%d\n", x);
    va_start (arglist, x);
    for (i=0; i<x; i++)
    result += va_arg ( arglist, double);
    va_end (arglist);
    return result;
    }
    /*
    well understand how to do functions but it's just this header file with this function I do not understand it. I know that the AddDouble function is called upon by the printe function, but I really don't know what the arguments mean. Also the for loop I do not understand it, there's also the result why is it initialized with 0 and "i" is not initialized before the loop, so please I need help, thank you. Can someone be kind enough as to explain to me how this works?*/
    Last edited by incognito; 10-24-2001 at 03:56 PM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    printf("The result returned by AddDouble() is: %2.1f\n n",AddDouble (1, d1));
    printf("Given an argument: %2.1f and %2.1f \n",d1, d2);
    printf("The result returned by AddDouble() is: %2.1f\n n",AddDouble (1, d1, d2));
    printf("Given an argument: %2.1f, %2.1f , and %2.1f \n",d1, d2, d3);
    printf("The result returned by AddDouble() is: %2.1f\n n",AddDouble (1, d1, d2, d3));

    Should be

    printf("The result returned by AddDouble() is: %2.1f\n n",AddDouble (1, d1));
    printf("Given an argument: %2.1f and %2.1f \n",d1, d2);
    printf("The result returned by AddDouble() is: %2.1f\n n",AddDouble (2, d1, d2));
    printf("Given an argument: %2.1f, %2.1f , and %2.1f \n",d1, d2, d3);
    printf("The result returned by AddDouble() is: %2.1f\n n",AddDouble (3, d1, d2, d3));

    The first parameter is the number of additional parameters (hence the for loop in AddDouble)
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Unhappy help

    I really need help with this codes it's gotten to the point, where I am actually thinking about quitting programming all together, I've been on this same thing for like 4 days now, I've been looking for answer everywhere.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    18
    I can't help you with the problem above yet, but I would suggest picking up a different book. I have tried a couple different versions of the "teach yourself <blank> in however long it is books" and have found them to be unclear and at times, flat out incorrect. Go to your local bookstore and look through their computing section for a book that feels comfortable to you.

    Don't give up on it yet, you may just need a new teacher

    Guideon72

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21