C: Dynamic string using variable number of arguments

Discussion in 'C Programming' started by negru, Mar 14, 2015.

  1. negru

    negru

    Joined:
    Mar 14, 2015
    Messages:
    1
    Likes Received:
    0
    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;}elseif(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;}

    elseif(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;}

    elseif(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;}elseif(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);return0;}

    I assume the error is in functions(itoa,fcvt,ecvt). Thanks for replies.
     
    negru, Mar 14, 2015
    #1
    1. Advertisements

  2. negru

    old-student

    Joined:
    Mar 22, 2015
    Messages:
    7
    Likes Received:
    2
    Location:
    SE Washington
    When you copy your code into your message it loses all the formatting that you used (if any). If you use the square bracket '[' and the word "code" followed by the closing square bracket ']' before your code and then after your code use '[' '/' "code" ']' it will be a lot easier to read.
     
    old-student, Mar 22, 2015
    #2
    Ian likes this.
    1. Advertisements

  3. negru

    jaime6996

    Joined:
    Mar 27, 2015
    Messages:
    1
    Likes Received:
    0
    can some one please help me wit this question? Where exacly do u enter this info to come up with the info needed to copie n past on to track 1 n 2 n on the magna stripp? Would it be in a pc's cmd command promp window? In a url address bar?
     
    jaime6996, Mar 27, 2015
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    1
    Views:
    479
    Ron Natalie
    Oct 16, 2003
  2. markus_e
    Replies:
    4
    Views:
    701
    Andre Kostur
    Apr 7, 2004
  3. Vince
    Replies:
    3
    Views:
    715
    Victor Bazarov
    Nov 10, 2004
  4. Ganny
    Replies:
    8
    Views:
    588
    Victor Bazarov
    Apr 7, 2005
  5. ankur
    Replies:
    1
    Views:
    15,022
    Jan =?UTF-8?B?VGhvbcOk?=
    Aug 27, 2007
  6. moreau.steve@gmail.com
    Replies:
    3
    Views:
    1,149
    moreau.steve@gmail.com
    Dec 31, 2008
  7. Navaneeth
    Replies:
    4
    Views:
    793
    Kenny McCormack
    Nov 20, 2010
  8. oldyork90
    Replies:
    10
    Views:
    671
    Jorge
    Sep 27, 2008
Loading...