Skip navigation

Category Archives: tech

Understanding va_list

Writing your own function that uses a va_list is really easy!

But first, let’s identify what a va_list is.

Think about the printf() C function.

printf(“Hello there! I like the numbers %d, %d and %d\n\n\n”, 1, 3, 7);

Obviously the output of that function call would be:

Hello there! I like the numbers 1, 3 and 7

But the key point here is, the printf() function can accept a VARYING NUMBER OF ARGUMENTS. That’s because it uses a va_list.

If you look at the signature for printf(), it looks like this:

int printf( char * format, … );

So the argument list for printf() has 2 main things:

  1. char * format – a regular string
  2. and a second special argument, … (3 dots, just like that)
  3. … is called an “ellipsis”, and it means, in plain English: “any number of optional arguments can go here.”

    So somehow, in the innermost bowels of printf(), is some sticky code that somehow retrieves each one of the the list of args you’re passing in, in the place of the “…”.

    Cool! So is it possible for us to write our functions that have their own sticky code that can process a set of VARIABLE ARGUMENTS???

    YES YOU CAN. And its actually simple!

    An example:

    #include <stdio.h>
    #include <stdarg.h>
    
    int addThemAll( int numargs, ... )
    {
        // So this function can accept a variable number
        // of arguments.  No (practically speaking) limits.
    
        // RULES you must know in order to be able to use "..." in one of your
        // own functions:
        //
        // 1)  The ... MUST appear exactly as ... 
        // It cannot be "..." (with the quotes),
        //     '...', or anything else weird.
        //
        // 2)  The ... __MUST GO LAST__ IN THE ARGUMENT LIST
        //
        // 3)  THERE MUST BE AT LEAST ONE MANDATORY, NON-OPTIONAL ARGUMENT,
        //     THAT COMES BEFORE THE ...
    
        // We'll be using these macros here:
    /*
         va_list    va_start    va_end    va_arg
    */
    
        // All of the above va_* things are actually special MACROS,
        // exclusively defined for us to use when working with
        // _V_ariable _A_rgument lists.
        
        // FIRST, we create a POINTER that will be used
        // to point to the first element of the VARIABLE
        // ARGUMENT LIST.
        va_list listPointer;
    
        // Currently, listPointer is UNINITIALIZED, however,
        // SO, now we make listPointer point to
        // the first argument in the list
        va_start( listPointer, numargs );
    
        // Notice that numargs is the LAST MANDATORY ARGUMENT
        // that the addThemAll() function takes.
        // By "LAST MANDATORY ARGUMENT", I mean 'numargs'
        // is the last argument to the addThemAll() function
        // JUST BEFORE the "..."
    
        // NEXT, we're going to start to actually retrieve
        // the values from the va_list itself.
        // THERE IS A CATCH HERE.  YOU MUST KNOW THE
        // DATA TYPE OF THE DATA YOU ARE RETRIEVING
        // FROM THE va_list.  In this example, I'm assuming
        // they're all ints, but you could always pass a format
        // string that lets you know the types.
    
        int sum = 0;
        for( int i = 0 ; i < numargs; i++ )
        {
            // GET an arg.  YOU MUST KNOW
            // THE TYPE OF THE ARG TO RETRIEVE
            // IT FROM THE va_list.
            int arg = va_arg( listPointer, int );
    
            printf( "    The %dth arg is %d\n", i, arg );
    
            sum += arg;
        }
        
        printf("--");
        printf("END OF ARGUMENT LIST\n\n");
        
        // FINALLY, we clean up by saying
        // va_end().  Don't forget to do this
        // BEFORE the addThemAll() function returns!
        va_end( listPointer );
    
        printf("The total sum was %d\n\n", sum);
    
        return sum;
    }
    
    int main()
    {
        // Try it out.
    
        printf("Calling 'addThemAll( 3, 104, 29, 46 );' . . .\n");
        addThemAll( 3, 104, 29, 46 );
    
        printf("Calling 'addThemAll( 8,   1, 2, 3, 4, 5, 6, 7, 8 );' . . .\n");
        addThemAll( 8,   1, 2, 3, 4, 5, 6, 7, 8 );
    
        return 0;
    }
    
    

    Download Visual Studio project files.

    (donation should be to bill.sherif@gmail.com)

I heard that wordpress was giving members 3 gb of space. . . however, i didn’t realize we’re still limited to just image files and ms-word docs.

Hmm. We need to serve up other file types. For instance, I’m wanting to serve up a few .zip packages that contain MS Visual Studio projects. These packages are in the KB, and if .doc files can be served up from the blog, shouldn’t code packages be too?

There are a number of options though

esnips.com

!!! My pick (I am moving to mediafire.com, Jan 1/09. I’ll still use esnips, but mediafire looks really neat.)
links like CLICK HERE TO DOWNLOAD!

ugh.. microsoft skydrive. I really DO NOT like how it downloads through a script request. its annoying!!

4shared.com

links like http://www.4shared.com/file/35910795/33c58b0a/testfile.html

I like 4shared.

Other fileupload hosts

wikifortio

Download links like http://www.wikifortio.com/799208/testfile.txt
1 – 99 day storage period
Upload seems to be a tad slow, but I only uploaded a small testfile

senduit

Links like http://senduit.com/b36ba8
– Longevity: 30 minute expiry
– Comments: Doesn’t meet my needs here, but i can see how this would be useful for distributing a smaller file quickly among 5 or 6 friends quickly and easily.

rapidshare.de

links like http://rapidshare.de/files/38405006/testfile.txt.html
Too many “hoops” to jump through to get to final download. Limited unless you have premium.

mihd.net

links like http://mihd.net/f0e2zv
pretty good

badongo.com

links like http://www.badongo.com/file/7479445

files.to

files.to is stupid because it requires a captcha

mediafire.com

WOW. This one is really glossy. Links like CLICK TO DOWNLOAD!. NO LOGIN REQUIRED TO UPLOAD! Really neat! (Tested Thurs Jan 1 / 09) !!! NEW PICK Jan 1 /09

STILL SEARCHING

I’m still looking for a file host that will give me a direct link that I can just post to my blog, without requiring redirection.

The closest thing to this that i’ve found is hfs, which allows you to easily set a folder to see the public internet.

It also does a good job of getting around routers, you just have to port forward.

The best thing would be . . .

Of course, the best thing would be if wordpress actually allowed users to upload other types of files. Although there’s potential for abuse, I want/need to upload code packages (.zip files that contain .cpp files).

WordPress COULD allow .zip archives up to a maximum of say, 8MB or so. Code packages will be something in the KB, so they won’t cause traffic problems.

Lots of people know you have to use &gt; in your HTML to get the > sign.

However, it appears that lots of people don’t know you can get superscripts and subscripts by using the <sup> and <sub> tags.

You can also get the ≤ sign by using &le; in your HTML.

I love this page.

more HTML entities than you thought existed.

And a unicode character reference for bloggers.

This artist muses on how good icons will not attract too much attention to themselves. Good icons will allow the user to communicate his will to the application program with an intuitive ease — kind of like ‘an electronic ouiji board’, he says.

http://www.paintedweb.com/web/?p=4