The offset of outbuf after iconv() will be advanced, so you need to do some pointer arithmetic to go back by the number of advanced output bytes, or maintain another pointer to the output buffer before iconv() and use that.
For example, this works on my system:
#include <stdio.h>
#include <stdlib.h>
#include <iconv.h>
int main()
{
iconv_t cd;
char instr[]="";
char *inbuf;
char *outbuf;
unsigned int insize = strlen(instr);
unsigned int outbufsize = 10;
unsigned int avail = outbufsize;
unsigned int nconv;
inbuf=instr;
outbuf=(char*)malloc(outbufsize);
memset(outbuf, '\0', outbufsize);
cd=iconv_open("GB2312", "UTF-8");
if(cd==(iconv_t)-1)
{
printf("fail.\n");
}
nconv=iconv(cd,&inbuf,&insize,&outbuf,&avail);
outbuf -= (outbufsize-avail);
printf("%s\n", outbuf);
printf("nconv is: %d\n", nconv);
free(outbuf);
return 0;
}
Seems like the return value of iconv() is not too meaningful except for checking error condition. Most programs I have seen using iconv() just throw it away.