question about va_list

Discussion in 'C Programming' started by gaoqiang, Oct 24, 2011.

  1. gaoqiang

    gaoqiang Guest

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdarg.h>
    int ssnprintf(char**arg,size_t n,const char*format,va_list va)
    {
    size_t len=snprintf(NULL,0,format,va);
    len+=1;
    if(len>n){
    free(*arg);
    *arg=malloc(len);
    }
    snprintf(*arg,len,format,va);
    return 0;
    }
    int main()
    {
    char*p=NULL;
    ssnprintf(&p,4,"abcdefgh%d",3);
    printf("p= %s\n",p);
    free(p);
    return 0;
    }


    compile this program with gcc,I got
    a.c:18: warning: passing argument 4 of 'ssnprintf' makes pointer from
    integer without a cast
    to say this line:
    ssnprintf(&p,4,"abcdefgh%d",3);

    then how to eliminate this warning ?
    gaoqiang, Oct 24, 2011
    #1
    1. Advertising

  2. gaoqiang

    Ike Naar Guest

    On 2011-10-24, gaoqiang <> wrote:
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <stdarg.h>
    > int ssnprintf(char**arg,size_t n,const char*format,va_list va)
    > {
    > size_t len=snprintf(NULL,0,format,va);
    > len+=1;
    > if(len>n){
    > free(*arg);
    > *arg=malloc(len);
    > }
    > snprintf(*arg,len,format,va);
    > return 0;
    > }


    You cannot use snprintf() like that, look into vsnprintf().
    Ike Naar, Oct 24, 2011
    #2
    1. Advertising

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. va_list in JNI

    , Jun 23, 2005, in forum: Java
    Replies:
    4
    Views:
    2,325
  2. Rich Herrick

    reference to va_list

    Rich Herrick, Jan 16, 2005, in forum: C++
    Replies:
    0
    Views:
    432
    Rich Herrick
    Jan 16, 2005
  3. Peter

    va_list help, please ...

    Peter, Feb 15, 2005, in forum: C++
    Replies:
    6
    Views:
    3,546
    Pete Becker
    Feb 15, 2005
  4. Rick Anderson

    varargs question: va_list in structure

    Rick Anderson, May 19, 2006, in forum: C Programming
    Replies:
    2
    Views:
    314
    Dave Thompson
    May 29, 2006
  5. Fred

    Question about va_list

    Fred, Jan 7, 2008, in forum: C Programming
    Replies:
    8
    Views:
    430
    CBFalconer
    Jan 7, 2008
Loading...

Share This Page