INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

C: Dynamic string using variable number of arguments

C: Dynamic string using variable number of arguments

C: Dynamic string using variable number of arguments

(OP)
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 --> C

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

RE: C: Dynamic string using variable number of arguments

Which compiler are you using? It should have told you that the parameter types for fcvt and ecvt are incorrect. The 2nd parameter is the number of decimal places/number of digits

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close