• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re(2): Variable argument list not working
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re(2): Variable argument list not working


  • Subject: Re(2): Variable argument list not working
  • From: Jens Bauer <email@hidden>
  • Date: Wed, 14 Aug 2002 16:33:38 +0200

Hi Pierre-Olivier,

On Tue, 13 Aug, 2002, Pierre-Olivier Latour <email@hidden> wrote:

>> On Tuesday, August 13, 2002, at 02:23 PM, Pierre-Olivier Latour wrote:
>>
>>> The problem is I need to pass a variable argument list, not a "va_list"
>>> argument? Is this possible?
>>
>> Yes. You'll need two variants of your function just like there is printf
>> and vprintf. Look at the prototypes for NSLog and NSLogV and you should
>> be able to take it from there.
>
>Thanks for the answer. However, I'm not sure I get it...
>
>Basically, you mean there's no way to call a function with this prototype
>from another function with this one?
>
>void foo1(char* a, ...)
>void foo2(char* b, ...)
>
>foo2 calling foo1 passing it the exact same arguments.
>
>That exactly what I'd like to do.

That's pretty difficult.
Instead you should..

void vfoo(char *a, va_list marker)
{
here you parse the actual arguments.
}

void foo(char *a, ...)
{
va_list marker;
va_start(a, marker);
vfoo(a, marker); /* recycle the above routine, so we don't
have to write it twice. */
va_end(marker);
}

>From what I've understood, I can only do this:
>void foo1(char* a, va_list list)
>void foo2(char* b, ...)
>
>Is this correct?

Nope, that's not completely correct. ;)

You can do this, if you want:

void foo(int a, ...);

-You are in complete control of how you want the arguments to work.

Say, you have a number of pointers you want to pass:

void foo(int format, ...)
{
va_list marker;
SInt16 mouse_x;
SInt16 mouse_y;
Uint8 mouse_k;

va_start(format, marker);

mouse_x = 0;
mouse_y = 0;
mouse_k = 0;
switch(format)
{
case 1:
mouse_x = va_arg(marker, SInt16);
mouse_y = va_arg(marker, SInt16);
printf("mouse position: (%d, %d)\n", mouse_x, mouse_y);
break;
case 2:
mouse_k = va_arg(marker, UInt8);
printf("button value:%d\n", mouse_k);
break;
default:
printf("Bad format passed to foo():%d\n", format);
}
va_end(marker);
}

You could also make "format" be a counter, telling how many parameters
are passed to the subroutine/method:

void bar(unsigned short count, ...)
{
va_list marker;
int i;

va_start(count, marker);

i = 0;
while(count--)
{
if(i++ < 4)
{
printf("Got an 8 bit value %d.\n", (int) va_arg(marker,
unsigned char));
}
else
{
printf("Got a 32 bit value %d.\n", (int) va_arg(marker, long));
}
}

va_end(marker);
}

Now in this case, I just made the list be a bunch of numbers, starting
with 4 unsigned chars, then the rest is longs.
It's completely up to you, how you want this to work.
Unfortunately, I don't think there's a legal way of avoiding the first
argument, however, you would always know the size of the first argument,
right ? -So you could just take that as a first parameter:

void bar(char param1, ...)
{
va_list marker;
long value;
int i;

va_start(count, marker);

i = 0;
value = param1;
while(value)
{
if(i++ & 1)
{
value = va_arg(marker, short);
}
else
{
value = va_arg(marker, long);
}
printf("Got a value: %d\n", value);
}

va_end(marker);
}

So it's very flexible.


Love,
Jens

--
Jens Bauer, Faster Software.
-Let's make the World better, shall we ?
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Re(2): Variable argument list not working
      • From: Pierre-Olivier Latour <email@hidden>
References: 
 >Re: Variable argument list not working (From: Pierre-Olivier Latour <email@hidden>)

  • Prev by Date: Re(2): URL string escape codes
  • Next by Date: NSRunLoop question
  • Previous by thread: Re: Variable argument list not working
  • Next by thread: Re: Re(2): Variable argument list not working
  • Index(es):
    • Date
    • Thread