本文介绍了C:使用可变数量的参数的动态字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我需要使用函数中可变数量的参数以给定格式(%d,%s,%f,%lf,%c)创建动态字符串。此代码给我一个错误(main.exe已停止工作): #include< stdio.h> #include< stdarg.h> #include< string.h> #include< stdlib.h> char * form( char * format,...); char * form( char * format,...) { char * res =( char *)calloc( 1 , 1 ),val_int [ 12 ],* pos_int,* val_str,* pos_str,* val_float,* pos_float,* val_lf,* pos_lf,* val_char,* pos_char; va_list args; va_start(args,format); do { pos_int = strstr(格式, %d); pos_str = strstr(格式, % s); pos_float = strstr(格式, %f); pos_lf = strstr(格式, %lf); pos_char = strstr(format, %c); if (pos_int&&(!pos_str || pos_int < pos_str)& ;&(!pos_float || pos_int < pos_float)&& (!pos_lf || pos_int < pos_lf)&&(!pos_char || pos_int < pos_char)) { itoa(va_arg (args, int ),val_int, 10 ); res =( char *)realloc(res,strlen(res)+(pos_int-format)+ strlen(val_int)+1); strncat(res,format,pos_int-format); strcat(res,val_int); format = pos_int + 2; } 其他 if (pos_str&&(!pos_int || pos_str < pos_int)&&(!pos_float || pos_str < pos_float)&& ; (!pos_lf || pos_str < pos_lf)&&(!pos_char || pos_str < ; pos_char)) { val_str = va_arg(args, char *); res =( char *)realloc(res,strlen(res)+(pos_str-format)+ strlen(val_str)+1); strncat(res,format,pos_str-format); strcat(res,val_str); format = pos_str + 2; } 其他 如果(pos_float&&( !pos_int || pos_float < pos_int)&&(!pos_str || pos_float < pos_str )&& (!pos_lf || pos_float < pos_lf)&&(!pos_char || pos_float < pos_char)) { fcvt(va_arg(args, double ),val_float, 6 , 0 ); res =( char *)realloc(res,strlen(res)+(pos_float-format)+ strlen(val_float)+1); strncat(res,format,pos_float-format); strcat(res,val_float); format = pos_float + 2; } 其他 if (pos_lf&&( !pos_int || pos_lf < pos_int)&&(!pos_str || pos_lf < pos_str )&& (!pos_float || pos_lf < pos_float)&&(!pos_char || pos_lf < pos_char)) { ecvt(va_arg(args, double ),val_lf, 6 , 0 ); res =( char *)realloc(res,strlen(res)+(pos_lf-format)+ strlen(val_lf)+1); strncat(res,format,pos_lf-format); strcat(res,val_lf); format = pos_lf + 2; } else if (pos_char&&(!pos_int || pos_char < pos_int)&&(!pos_str || pos_char < pos_str)&& ; (!pos_float || pos_char < pos_float)&&(!pos_lf || pos_char < ; pos_lf)) { val_char = va_arg(args, char *); res =( char *)realloc(res,strlen(res)+(pos_char-format)+ strlen(val_char)+1); strncat(res,format,pos_char-format); strcat(res,val_char); format = pos_char + 2; } } while (pos_int || pos_str || pos_float || pos_lf || pos_char); va_end(args); res =( char *)realloc(res,strlen(res)+ strlen(format)+1); strcat(res,format); return res; } int main() { char * s; s = form( John是%d岁,在%s中有%c。他要去学习%lf年。, 9 ,' A', maths, 1 。 5 ); printf( %s,s); free(s); return 0 ; } 我假设错误在函数中(itoa,fcvt,ecvt)。感谢您的回复。解决方案 您正在某种程度上重新发明轮子。对于变量争论者来说,C ++中的函数是内置的。对于你的问题,带有示例代码的vsnprintf 是合适的。 PS:我认为如果您没有仔细调试它,那么错误就在您的代码中。 :-O I need to create dynamic string by given format(%d,%s,%f,%lf,%c) using variable number of arguments in function. This code gives me an error(main.exe has stopped working):#include<stdio.h>#include<stdarg.h>#include<string.h>#include<stdlib.h>char *form(char *format,...);char *form(char *format,...){ char *res=(char *)calloc(1,1),val_int[12],*pos_int,*val_str,*pos_str,*val_float,*pos_float,*val_lf,*pos_lf,*val_char,*pos_char; va_list args; va_start(args,format); do { pos_int=strstr(format,"%d");pos_str=strstr(format,"%s");pos_float=strstr(format,"%f");pos_lf=strstr(format,"%lf");pos_char=strstr(format,"%c"); if(pos_int && (!pos_str || pos_int < pos_str) && (!pos_float || pos_int < pos_float) && (!pos_lf || pos_int < pos_lf) && (!pos_char || pos_int < pos_char)) { itoa(va_arg(args,int),val_int,10); res=(char *)realloc(res,strlen(res)+(pos_int-format)+strlen(val_int)+1); strncat(res,format,pos_int-format); strcat(res,val_int); format=pos_int+2; } else if(pos_str && (!pos_int || pos_str < pos_int) && (!pos_float || pos_str < pos_float) && (!pos_lf || pos_str < pos_lf) && (!pos_char || pos_str < pos_char)) { val_str=va_arg(args,char *); res=(char *)realloc(res,strlen(res)+(pos_str-format)+strlen(val_str)+1); strncat(res,format,pos_str-format); strcat(res,val_str); format=pos_str+2; } else if(pos_float && (!pos_int || pos_float < pos_int) && (!pos_str || pos_float < pos_str) && (!pos_lf || pos_float < pos_lf) && (!pos_char || pos_float < pos_char)) { fcvt(va_arg(args,double),val_float,6,0); res=(char *)realloc(res,strlen(res)+(pos_float-format)+strlen(val_float)+1); strncat(res,format,pos_float-format); strcat(res,val_float); format=pos_float+2; } else if(pos_lf && (!pos_int || pos_lf < pos_int) && (!pos_str || pos_lf < pos_str) && (!pos_float || pos_lf < pos_float) && (!pos_char || pos_lf < pos_char)) { ecvt(va_arg(args,double),val_lf,6,0); res=(char *)realloc(res,strlen(res)+(pos_lf-format)+strlen(val_lf)+1); strncat(res,format,pos_lf-format); strcat(res,val_lf); format=pos_lf+2; } else if(pos_char && (!pos_int || pos_char < pos_int) && (!pos_str || pos_char < pos_str) && (!pos_float || pos_char < pos_float) && (!pos_lf || pos_char < pos_lf)) { val_char=va_arg(args,char *); res=(char *)realloc(res,strlen(res)+(pos_char-format)+strlen(val_char)+1); strncat(res,format,pos_char-format); strcat(res,val_char); format=pos_char+2; } } while(pos_int || pos_str || pos_float || pos_lf || pos_char); va_end(args); res=(char *)realloc(res,strlen(res)+strlen(format)+1); strcat(res,format); return res;}int main(){ char *s; s=form("John is %d years old and has an %c in %s. He is going to school for %lf years.",9,'A',"maths",1.5); printf("%s",s); free(s); return 0;}I assume the error is in functions(itoa,fcvt,ecvt). Thanks for replies. 解决方案 You are somehow "reinventing the wheel". For variable argumenst are functions in C++ build in. For your problem is vsnprintf with example code suitable.PS: I assume that the error is in your code if you havent debugged it REALLY carefuly. :-O 这篇关于C:使用可变数量的参数的动态字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 18:04