Link to home
Start Free TrialLog in
Avatar of kishk91
kishk91

asked on

Function(char ch,int y,...) how to use this kinda function?

Hi guys..
i have seen this kind of function and i would like to understand how to use it..
i think that its a funtcion that you can pass to it as many prams as you need (in this case int type only)

how do i extract the data ??
May thanks
kishk91
Avatar of chensu
chensu
Flag of Canada image

The parameters can be any data types. You know how to use it. Have you ever used the following function?

int printf(const char *format, ...);
Avatar of kishk91
kishk91

ASKER

Hi..
yes i know printf..
but how do i build my own unction like this??

Thanks.
Avatar of kishk91

ASKER

Hi..
yes i know printf..
but how do i build my own unction like this??

Thanks.
Hi,

This sample program may give u some idea. compile and run it.

#include <iostream.h>

int funct(char ch,int x,...);

void main()
{
      funct('f',1,200,3);
      return;
}

int funct(char cc,int p,...)
{
      int x = p;
      int y = *(&p+1);
      int z = *(&p+2);

      cout << "First Arg = " << cc << endl;
      cout << "Second Arg = " << x << endl;
      cout << "Third Arg = " << y << endl;
      cout << "Fourth Arg = " << z << endl;
      return 1;
}

Hope that gives U some idea on how to use such functions.
ASKER CERTIFIED SOLUTION
Avatar of me8873
me8873

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kishk91

ASKER

Thanks...
it works.. can you give me some idea how to loop around and collect all the passed vars?
Avatar of kishk91

ASKER

Hi. ok thanks it work ok but i have one problem:
this is my code:

void CMyClass::Func(int y,...)
{
int Sum=0;
  va_list pStrList;
  va_start(pStrList,y);
  int u=0;
 
  Sum+=y ;

  while((Sum+=va_arg(pStrList,int))!= NULL)
      u++;

  va_end(pStrList);
}
i did it just for testing.. and i get never ending loop..
it never reaches NULL..
why?

Please email me the answer to:
kishk91@hotmail.com
Thanks
kishk91
Hi,

If you want to get NULL, the last parametor to the function must be NULL.

Generaly you can handle such function in two ways:

1) Give another parameter telling the number of parameters, then loop this number times.

2) Set the last parameter to be out-of-renge (like NULL) value, and loop till you rich it.
Avatar of kishk91

ASKER

Thanks for your help...
its ok now..
>but how do i build my own unction like this??

Your original question says "i would like to understand how to use it..".
Avatar of kishk91

ASKER

Hi chensu...
ok imsorry i wasnt explaining my self correctly...

no heart feelings?
No problem. Next time, in order to get quick and good answer, you should state the question clearly.
Avatar of kishk91

ASKER

I got it ...

;-)