Mailing List Archive

Support open source code!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

sprintf



A little C problem for all you gurus out there.

In most compilers (eg. Borland,Microsoft,Linux GCC,Sparc C++ compiler)
sprintf is defined as:

int sprintf(char *buf,const char *fmt,...);      //Returns number of
characters put into buf.

So in my code I often use this kind of code to append strings.

char *p=buf;
p+=sprintf(p,"First line\n");
p+=sprintf(p,"Second line\n");
//etc.


But GCC 2.7.2 on a Sun has it defined (in stdio.h) as:

extern char	*sprintf _PARAMS((char *, const char *, ...));

Returning a char* screws up my code. All other printf functions (even
vsprintf) return the int! 

Q1. Is the char* it is returning a pointer to the beginning of the buffer,
or to the end of the buffer when it has finished?

Q2. Is there a way I can get round this?

My best idea so far is to replace all occurences in my code of sprintf with
mysprintf, and then put this code into a header file:
#if GCC_ON_SUN
int mysprintf(char *buf,const char *fmt,...){
    va_list argptr;
    va_start(argptr,fmt);
    int sz=vsprintf(buf,fmt,argptr);
    va_end(argptr);
    return sz;
}

#else
#define mysprintf sprintf
#endif

Is there a way to do something similar without having to rename sprintf in
my code?

Thanks in advance.

Darren




-----------------------------------------------------------------
a word from the sponsor will appear below
-----------------------------------------------------------------
The TLUG mailing list is proudly sponsored by TWICS - Japan's First
Public-Access Internet System.  Now offering 20,000 yen/year flat
rate Internet access with no time charges.  Full line of corporate
Internet and intranet products are available.   info@example.com
Tel: 03-3351-5977   Fax: 03-3353-6096


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links