Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Untitled
Friday, March 30th, 2012 at 6:25:40am UTC 

  1. mqudsi@debian:~/TripleHelix$ cat test.c
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <stdlib.h>
  6.  
  7. char *myPrintfInner(const char *message, va_list params)
  8. {
  9.         va_list args_copy;
  10.         va_copy(args_copy, params);
  11.  
  12.     size_t length = vsnprintf(NULL, 0, message, args_copy);
  13.     va_end(args_copy);
  14.    
  15.     char *final = (char *) malloc((length + 1) * sizeof(char));
  16.     int result = vsnprintf(final, length + 1, message, params);
  17.  
  18.     printf("vsnprintf result: %d\r\n", result);
  19.     printf("%s\r\n", final);
  20.  
  21.     return final;
  22. }
  23.  
  24. char *myPrintf(const char *message, ...)
  25. {
  26.     va_list va_args;
  27.     va_start(va_args, message);
  28.  
  29.     size_t length = vsnprintf(NULL, 0, message, va_args);
  30.     char *final = (char *) malloc((length + 1) * sizeof(char));
  31.     int result = vsnprintf(final, length + 1, message, va_args);
  32.  
  33.     printf("vsnprintf result: %d\r\n", result);
  34.     printf("%s\r\n", final);
  35.  
  36.     //return final;
  37.     va_end(va_args);
  38.  
  39.     return final;
  40. }
  41.  
  42. int main(int argc, char **argv)
  43. {
  44.     char *test = myPrintf("This is a %s.", "test");
  45.     char *actual = "This is a test.";
  46.     int result = strcmp(test, actual);
  47.  
  48.     if (result != 0)
  49.     {
  50.         printf("%d: Test failure!\r\n", result);
  51.     }
  52.     else
  53.     {
  54.         printf("Test succeeded.\r\n");
  55.     }
  56.  
  57.     return 0;
  58. }
  59. mqudsi@debian:~/TripleHelix$ gcc  -Wall --std=c99 test.c -o test && ./test
  60. vsnprintf result: 17
  61. This is a �v�s�
  62. 14: Test failure!
  63. mqudsi@debian:~/TripleHelix$

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

comments powered by Disqus