Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

chromatic (983)

chromatic
  (email not shown publicly)
http://wgz.org/chromatic/

Blog Information [technorati.com] Profile for chr0matic [technorati.com]

Journal of chromatic (983)

Thursday May 10, 2007
01:55 AM

Hateful C Compiler Magic

[ #33242 ]

Suppose that I want to call a C function in a shared library. Suppose also that the only way I can call this function is through a function pointer.

That part I know how to do. That's solvable.

Now suppose that I want to call a varargs function, and the same caveats apply. I have to call this function through a function pointer.

How do I do it? Do I build my own va_list structure by hand? If so, how?

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • It's actually pretty straightforward:

    #include <stdio.h>
    #include <stdarg.h>

    void bar(int cnt, ...) {
        va_list ap;
        va_start(ap,cnt);
        while (cnt--) {
            int a = va_arg(ap, int);
            printf("%d ", a);
        }
        printf("\n");
    }

    int main() {
        void (*b)(int, ...);
        b = bar;
        b(2,1,2);
        b(3,1,2,3);
        return 0;
    }

    Or did I miss something?
    • Or did I miss something?

      Hmm, no. I missed b = bar; in my example, which leads me to wonder why there was no crash.

      Thanks for the sanity check! Now I need to figure out how to add varargs support to NCI.