+ -
当前位置:首页 → 问答吧 → 变长参数格式化,还是段错误问题

变长参数格式化,还是段错误问题

时间:2010-08-09

来源:互联网

只是想调用start后,把所有必需参数和变长参数格式化成%s|%s|%s|%s.....
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdarg.h>
  4. #include<malloc.h>
  5. void Start(char *path,char *name,int n,...)
  6. {
  7.         char sockbuf[1024];
  8.         char *tempbuf;
  9.         tempbuf=(char*)malloc(20*sizeof(tempbuf));
  10.         char buf[100];
  11.         va_list ap;
  12.         int i;
  13.         va_start(ap,n);
  14.         for(i=0;i<n;i++){
  15.                 tempbuf=va_arg(ap,char *);
  16.                 strcat(tempbuf,"|");
  17.                 printf("%s\n",tempbuf);
  18.         }
  19.         va_end(ap);
  20.         sprintf(sockbuf,"%s|%s%s",path,name,buf);
  21.         printf("%s",sockbuf);
  22. }

  23. int main(void)
  24. {
  25.         Start("/home/Alone/task/exe/JavaTest","JavaTest",4,"hello","world","liheng","yes");
  26. }
复制代码

作者: Mr-Summer   发布时间: 2010-08-09

  1. tempbuf=va_arg(ap,char *); /* 都是char * */
  2.                 strcat(tempbuf,"|");
  3.                 printf("%s\n",tempbuf)


  4. Start("/home/Alone/task/exe/JavaTest","JavaTest",4,"hello","world","liheng","yes");/*有个4*/
复制代码

作者: zhangsuozhu   发布时间: 2010-08-09