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,

CodePudding user response:

reference 1st floor qq_39850605 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,


This method also can be, but I've written to SumEx is function, want to reuse directly, if use your method, have to rewrite SumEx function,

CodePudding user response:

The pArgs or the backup as parameters to the called function inside?

CodePudding user response:

You this is not the place, the use of variable parameter but use an array of place, just for the length of the array input

CodePudding user response:

Since the piSum is an array, of course, it should be a "array as parameter of function,"
2 is not "the function of variable parameter number,"

The difference is:
"The function of variable parameter number is:" don't know when the function definition has a few parameters, but clearly know where the calling function has several parameters,
"Array as parameter of function is:" function at the time of definition, and call all don't know there are several data, to run when I know that there are a few data,
And he is essentially the uncertain data into an array parameter,
In this way, the number of parameters to determine, but the number of data is variable,

CodePudding user response:

If SumEx has identified is the form of variable parameter (such as the function was developed by others),
So you can only such
Int iSum=0;
The switch (iCnt)
{
Case 1: iSum=SumEx (iCnt, piSum [0]). break;
Case 2: iSum=SumEx (iCnt, piSum [1]), piSum [1]). break;
Case 3: iSum=SumEx (iCnt, piSum [1]), piSum [1], piSum [2]); break;
Case 4: iSum=SumEx (piSum iCnt, [1]), piSum [1], piSum [2], piSum [3]); break;
//...

}

CodePudding user response:

My understanding is that you want to pass the variable parameter is right, va_list pArgs; It is ok to use this as a parameter,

CodePudding user response:

This is difficult, va_arg macro is according to the function of stack variable parameters (the first) address to calculate the rest of the variable parameter, if the parameter list information wrong (insufficient stack memory information), that can't get right,
So the caller can't written in the correct format (the correct format as 6 l say, the more tired), va_arg is get parameters,
So this kind of circumstance is to write a function directly into your array as well,
  • Related