Thursday, May 23, 2013

Variadic Function Example #2

// This is just a quick sample of another very basic variadic function; this one returns which of however many // strings are sent to it is the longest.  I think that's probably a poorly phrased sentence, but there ya go.

#include <iostream>
#include <stdarg.h>
using namespace std;
const char* longest(char* lon,...);
int main(){
    char* lon;
    int g;
    cout << longest(lon,"He","heee","heeeeee",'\0');
    cin >> g;
    return 0;
}
const char* longest(char* lon,...){
    va_list whi;
    va_start(whi,lon);
    int i=0,j=0,k=0;
    char* bonk;
    char* bink;
    while(bonk=va_arg(whi,char*)){
        for(i=0;bonk[i];i++){
            j++;
            if(j>k)bink=bonk;
        }
    }
    return bink;
}

No comments:

Post a Comment