Home > Back-end > About the problem of variable parameter function called a variable parameter function
About the problem of variable parameter function called a variable parameter function
Time:09-24
I have a variable parameter function, is needed in this function according to the results of the incoming parameters calculated, then the result under a variable parameter, due to the number of results not sure, so I don't know how to pass on to the next function, Some people say that a similar problem before, but it is directly under the variable parameter passed to a function, so the problem and I have different, Below is an example of writing program, warrior, please help have a look, thank you! Int SumEx (int iCnt,... ) { Va_list pArgs; Va_start (pArgs iCnt); Int iSum=0; for (int i=0; I & lt; ICnt; I++) { Int iData=https://bbs.csdn.net/topics/va_arg (pArgs, int); ISum +=iData; Printf (" % d parameter=% d \ n ", I, iData); } Va_end (pArgs); Printf (" params=% d, and=% d \ n ", iCnt, iSum); Return iSum; } Int the Sum (int iCnt,... ) { Va_list pArgs; Va_start (pArgs iCnt); Int * piSum=new int [iCnt]; Memset (piSum, 0, sizeof (int) * iCnt); for (int i=0; I & lt; ICnt; I++) { INT8 * pBufferNew=va_arg (pArgs, INT8 *); Int iLen=va_arg (pArgs, int); For (int j=0; J & lt; ILen; J++) { PiSum [I] +=pBufferNew [j]; } } Va_end (pArgs); //the value of piSum one by one to Sum1 Int iSum=SumEx (iCnt); //parameters how to write the place? The delete piSum; PiSum=NULL; Return iSum;
}
CodePudding user response:
You don't have to use this way to the second variable parameter function assignment, you can define a container, to accommodate the uncertainty of it, put the results in the container in the first function, and then pass reference to a function, so there is no need of a given number of parameters when calling function, the next function directly traversal container will know that there are several parameters and values,