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
kishk91Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
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, ...);
0
kishk91Author Commented:
Hi..
yes i know printf..
but how do i build my own unction like this??

Thanks.
0
kishk91Author Commented:
Hi..
yes i know printf..
but how do i build my own unction like this??

Thanks.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Vinayak KumbarSr Program ManagerCommented:
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.
0
me8873Commented:
Hi,

You can use va_arg, va_end, va_start
to do it very easily. (you should include the stdarg.h header file)

For example,

void print(CString str,...) {
      cout << str << endl;
      CString arg;
      va_list pStrList;
      va_start(pStrList,str);
      while((arg = va_arg(pStrList,CString)) != NULL) {
            cout << arg << endl;
      }
      va_end(pStrList);
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kishk91Author Commented:
Thanks...
it works.. can you give me some idea how to loop around and collect all the passed vars?
0
kishk91Author Commented:
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
0
me8873Commented:
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.
0
kishk91Author Commented:
Thanks for your help...
its ok now..
0
chensuCommented:
>but how do i build my own unction like this??

Your original question says "i would like to understand how to use it..".
0
kishk91Author Commented:
Hi chensu...
ok imsorry i wasnt explaining my self correctly...

no heart feelings?
0
chensuCommented:
No problem. Next time, in order to get quick and good answer, you should state the question clearly.
0
kishk91Author Commented:
I got it ...

;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.