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 ?
 
Ad

Advertisements

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

C pipe 1
[va_list *] Is this legal ? 33
compressing charatcers 35
C language. work with text 3
string reversal 4
Wrapping a variadic function 16
wcstombs() problem 16
question regarding ++ operator. 7

Top