GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Mar-2015, 09:48
negru negru is offline
New Member
 
Join Date: Mar 2015
Posts: 1
negru is on a distinguished road

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

CPP / C++ / C 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  
Old 27-Oct-2015, 01:04
lindseynicole lindseynicole is offline
New Member
 
Join Date: Oct 2015
Posts: 14
lindseynicole has a little shameless behaviour in the past

Re: C: Dynamic string using variable number of arguments


void format_string(char *fmt, ...);

void debug_print(int dbg_lvl, char *fmt, ...) {
format_string(fmt, /* how do I pass all the arguments from '...'? */);
fprintf(stdout, fmt);
}
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception in thread "main" java.lang.NullPointerException at Cameras.sortByName(Came Dan0756 Java Forum 1 24-Aug-2012 19:35
Binary Tree recursive Functions 90fmm C++ Forum 3 27-Apr-2011 18:37
NetBeans - Inventory Program biglez123 Java Forum 1 23-Jan-2011 20:48
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 20:25
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 06:19

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:36.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.