0

I am confusing about one question.See an example:

-(void)DIYLog:(NSString *)format, ...
{
   NSLog(...);
}

It's just an example for fun. We all know that we can't pass "..." as parameters for NSLog. So I'm Curious about passing one "variable parameters" to another. I already know that params are passed by register or stack, But, The key point is , how can I implement it in Objective-C or C.


I think I make you misunderstand. NSLog is just an example to be explained. Let me make another. It's about passing params to id objc_msgSend(id self, SEL op, ...).

-(void)DIY_msgSend:(id)target selector:(SEL)op params:(id)param, ...
{
    objc_msgSend(target, op, ...);
}

So, the key is, how I can pass those variable parameters to another function which also need variable parameters.

Ringo_D
  • 81
  • 7
  • 1
    I dont understand your question at all, can you be more explicit and write more examples? – Antonio MG Oct 04 '13 at 15:11
  • 1
    I think the question is "How can I write a method that takes variable parameters pass those parameters to another function like NSLog." – Charlie Burns Oct 04 '13 at 15:13
  • 1
    Whether parameters are passed on the stack or in registers is entirely an implementation detail of the targeted architecture and the parameters being passed.... – bbum Oct 04 '13 at 15:31
  • This is not the thing I need. :) – Ringo_D Oct 06 '13 at 07:38

1 Answers1

4

The C va_list type can be created from variadic arguments and passed to functions/methods that accept a va_list parameter. Eg:

- (void)logFormat:(NSString *)format, ...
{
    va_list args;
    va_start(args, format);
    NSLogv(format, args);
    va_end(args);
}

However, there is no portable way of passing a va_list to a standard variadic function as you want to do.

Chris Devereux
  • 5,453
  • 1
  • 26
  • 32