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
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
ASKER
Hi..
yes i know printf..
but how do i build my own unction like this??
Thanks.
yes i know printf..
but how do i build my own unction like this??
Thanks.
ASKER
Hi..
yes i know printf..
but how do i build my own unction like this??
Thanks.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks...
it works.. can you give me some idea how to loop around and collect all the passed vars?
it works.. can you give me some idea how to loop around and collect all the passed vars?
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(pStrLis t,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
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(pStrLis
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.
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.
ASKER
Thanks for your help...
its ok now..
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..".
Your original question says "i would like to understand how to use it..".
ASKER
Hi chensu...
ok imsorry i wasnt explaining my self correctly...
no heart feelings?
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.
ASKER
I got it ...
;-)
;-)
int printf(const char *format, ...);