本文介绍了可变数量的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我阅读了关于传递可变数量参数的C FAQ问题, 但它没有帮助。该示例假设所有参数都是相同的 类型。 我想创建一个函数trace可以像这样使用: trace(" Err",errtType,lineNum,NULL)/ *其中errType是char *和 lineNum是 一个int * / trace(" Err",lineNum,NULL); trace( lineNum,Err,funcName,NULL)/ *其中funName是char * * / 问题是我不知道 传递的参数。 如何实现跟踪? 解决方案 I read the C FAQ question on passing a variable number of arguments,but it didn''t help. The example assumes all arguments are of the sametype.I want to create a function "trace" that can be used like this:trace( "Err", errtType, lineNum, NULL) /* where errType is char* andlineNum isan int */trace("Err", lineNum, NULL);trace( lineNum, "Err", funcName, NULL) /* where funName is char * */The problem is that I don''t know the order and the variable types ofthe arguments passed.How can I implement trace? 解决方案You can''t. trace() has to know the types of the arguments, or beable to figure them out.--int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\);}return 0;} 这篇关于可变数量的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-29 17:30
查看更多