question about va_list

G

gaoqiang

#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 ?
 
I

Ike Naar

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top