Thread: C: Dynamic string using variable number of arguments

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    6

    C: Dynamic string using variable number of arguments

    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):

    Code:
    #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.

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Trying to compile your program gives me:

    Code:
    main.c:37:10: warning: passing argument 2 of ‘fcvt’ makes integer from pointer without a cast [enabled by default]
              fcvt(va_arg(args,double),val_float,6,0);
              ^
    In file included from main.c:4:0:
    /usr/include/stdlib.h:818:14: note: expected ‘int’ but argument is of type ‘char *’
     extern char *fcvt (double __value, int __ndigit, int *__restrict __decpt,
                  ^
    main.c:37:10: warning: passing argument 3 of ‘fcvt’ makes pointer from integer without a cast [enabled by default]
              fcvt(va_arg(args,double),val_float,6,0);
              ^
    In file included from main.c:4:0:
    /usr/include/stdlib.h:818:14: note: expected ‘int * __restrict__’ but argument is of type ‘int’
     extern char *fcvt (double __value, int __ndigit, int *__restrict __decpt,
                  ^
    main.c:47:11: warning: passing argument 2 of ‘ecvt’ makes integer from pointer without a cast [enabled by default]
               ecvt(va_arg(args,double),val_lf,6,0);
               ^
    In file included from main.c:4:0:
    /usr/include/stdlib.h:812:14: note: expected ‘int’ but argument is of type ‘char *’
     extern char *ecvt (double __value, int __ndigit, int *__restrict __decpt,
                  ^
    main.c:47:11: warning: passing argument 3 of ‘ecvt’ makes pointer from integer without a cast [enabled by default]
               ecvt(va_arg(args,double),val_lf,6,0);
               ^
    In file included from main.c:4:0:
    /usr/include/stdlib.h:812:14: note: expected ‘int * __restrict__’ but argument is of type ‘int’
     extern char *ecvt (double __value, int __ndigit, int *__restrict __decpt,
    Honestly, these warnings are clear enough but you go ahead to run your program

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. variable number of arguments
    By newC72 in forum C Programming
    Replies: 6
    Last Post: 06-25-2012, 12:54 PM
  2. Function with variable number of arguments
    By andrewg in forum C Programming
    Replies: 9
    Last Post: 12-18-2010, 09:19 PM
  3. Variable number of arguments in function.
    By kamoda_pawel in forum C Programming
    Replies: 1
    Last Post: 01-18-2007, 07:18 AM
  4. Variable number of arguments
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-10-2004, 08:58 AM
  5. Functions with a variable number of arguments
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2002, 01:12 AM

Tags for this Thread