# Problem trying to use varargs in private function within C e

**URL:** <https://www.ruby-forum.com/t/problem-trying-to-use-varargs-in-private-function-within-c-e/89201>\
**Category:** Ruby\
**Created:** [March 18, 2007, 4:10pm UTC](https://www.ruby-forum.com/t/problem-trying-to-use-varargs-in-private-function-within-c-e/89201 "2007-03-18T16:10:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [March 18, 2007, 4:10pm UTC](https://www.ruby-forum.com/t/problem-trying-to-use-varargs-in-private-function-within-c-e/89201/1 "2007-03-18T16:10:34Z")

</div>

Hi all,

Ruby 1.8.6  
Windows XP  
VC++ 8

This simple C extension complains of “too few arguments for call” when  
I try to build it. Yet, within a standalone C program it works fine.  
Am I forced to use varargs.h instead of stdarg.h? If so, how do I make  
this work? The varargs.h docs confuse me.

#include \<ruby.h\>  
#include \<stdarg.h\>

int test(const char\*, …);

/\* String mandatory, encoding optional _/  
int test(const char_ str, int encoding, …){  
int total = 0;  
total = encoding;

printf(“X: %s\n”, str);  
printf(“Encoding: %i\n”, encoding);

return total;  
}

static VALUE foo\_test(VALUE self){  
int total = 0;

total = test(“hello”); /\* Failure! \*/  
printf(“TOTAL: %i\n”);  
return self;  
}

void Init\_foo(){  
VALUE cFoo = rb\_define\_class(“Foo”, rb\_cObject);  
rb\_define\_method(cFoo, “test”, foo\_test, 0);  
}

And here’s the standalone C code that builds and runs fine:

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

int test(const char\*, …);

int main(){  
int val = 0;

val = test(“hello”, 3);  
printf(“VAL: %i\n”, val);

return 0;  
}

int test(const char\* x, int encoding, …){  
int total = 0;  
total = encoding;

printf(“Encoding: %i\n”, encoding);

return total;  
}

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [March 18, 2007, 11:15pm UTC](https://www.ruby-forum.com/t/problem-trying-to-use-varargs-in-private-function-within-c-e/89201/2 "2007-03-18T23:15:25Z")

</div>

On Mar 18, 10:01 am, “Patrick H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> total = test(“hello”, 3);
> 
> Does it compile?  
> pth

Yes, the C code compiles either way. The Ruby code will only compile  
if I provide the 2nd argument.

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [March 18, 2007, 5:02pm UTC](https://www.ruby-forum.com/t/problem-trying-to-use-varargs-in-private-function-within-c-e/89201/3 "2007-03-18T17:02:41Z")

</div>

Dan,

In you example pure C code you are calling test with 2 parameters in  
your ruby code, only one. Your prototype has one mandatory param, and  
the function actually has two. If you change:

total = test(“hello”);

to

total = test(“hello”, 3);

Does it compile?  
pth

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [March 19, 2007, 12:51am UTC](https://www.ruby-forum.com/t/problem-trying-to-use-varargs-in-private-function-within-c-e/89201/4 "2007-03-19T00:51:34Z")

</div>

On 3/18/07, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> Dan

Then, my next guess is that it is the order of the prototypes and the  
actual function. In the C example you have a prototype (one param),  
use, definition (with two+ params). In the Ruby example you have  
prototype, definition (2+ params, over rides prototype), use. Move the  
init\_foo, up where your main is in the stand alone example.

FYI accessing the second parameter if you do not pass it, is likely to  
cause a memory exception on many platforms (depending upon call depth,  
build mode, etc).

pth

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [March 19, 2007, 1:15am UTC](https://www.ruby-forum.com/t/problem-trying-to-use-varargs-in-private-function-within-c-e/89201/5 "2007-03-19T01:15:36Z")

</div>

In article [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid),  
“Daniel B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Hi all,
> 
> Ruby 1.8.6  
> Windows XP  
> VC++ 8
> 
> This simple C extension complains of “too few arguments for call” when  
> I try to build it. Yet, within a standalone C program it works fine.  
> Am I forced to use varargs.h instead of stdarg.h? If so, how do I make  
> this work? The varargs.h docs confuse me.

Let me add a few comments in addition to Patrick H.'s correction (in  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)):

First, varargs.h will not help. In fact, you don’t even need  
stdarg.h until you actually start to reference the arguments using  
the appropriate macros.

Second, turn on the compiler diagnostics. _Both_ of your programs  
below are not legal C and any reasonable compiler should diagnose  
the problem(s). In particular, your declarations and definitions  
of “test” differ. Your declaration declares the procedure to have  
one mandatory argument; your definition requires that there are  
two mandatory arguments. These are inconsistent (and thus illegal).

Third, relevant to Patrick’s correction, note that your “stand-alone”  
test case is not at all the same as the in-context code (the number  
of arguments supplied is different).

> #include \<ruby.h\>  
> #include \<stdarg.h\>
> 
> int test(const char\*, …);

> /\* String mandatory, encoding optional _/  
> int test(const char_ str, int encoding, …){  
> int total = 0;  
> total = encoding;
> 
> printf(“X: %s\n”, str);  
> printf(“Encoding: %i\n”, encoding);
> 
> return total;  
> }

Running with spirit of this comment:

/\* String mandatory, encoding optional _/  
int test(const char_ x, …){  
va\_list ap;  
int total = 0;  
int encoding;

va\_start (ap, x);  
encoding = va\_arg(ap, int);  
va\_end(ap);

total = encoding;

printf(“Encoding: %i\n”, encoding);

return total;  
}

> rb\_define\_method(cFoo, “test”, foo\_test, 0);  
> }
> 
> And here’s the standalone C code that builds and runs fine:

Just because it builds and runs fine doesn’t mean that it isn’t  
wrong. That mind set just leads to future problems.

> printf(“VAL: %i\n”, val);  
> return total;  
> }
> 
> Dan

- dmw
