• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

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
0
kishk91
Asked:
kishk91
  • 7
  • 3
  • 2
  • +1
1 Solution
 
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
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
 
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 7
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now