Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems with sprintf()

Status
Not open for further replies.

huskers

Programmer
Jan 29, 2002
75
US
I am trying to print the sql values in a buffer and then try to print it. But the prbuf in the below chunk does not print anything. prbuf is defined as below:

char returnbuf[2023] = "No Info found!\n" ;
char *prbuf = &returnbuf[0] ;

case SQL_TYP_NINTEGER: /* long with null indicator */
prbuf += sprintf(prbuf, "%ld", *(long *) ptr) ;
/** this does not work **/
printf("buffer: %s\n", prbuf ) ;
lptr = (long *) ptr ;
/** this prints a value **/
printf("value: %*ld\n", maxCollen, *lptr ) ;
break ;

Am I missing anything or is there any problem with sprintf funtion.

Thanks
 
prbuf is pointing to the start of the buffer and you're using [tt]sprintf[/tt] to insert the long into it (null terminated.) In the same statement (with [tt]prbuf +=[/tt]) you're incrementing prbuf by the length of the string inserted, so it now points at the null. That's what you're printing with the printf.

The following would work:
[tt]char returnbuf[2023] = "No Info found!\n" ;
char *prbuf = &returnbuf[0] ;

case SQL_TYP_NINTEGER: /* long with null indicator */
sprintf(prbuf, "%ld", *(long *) ptr) ;
printf("buffer: %s\n", prbuf ) ;
[/tt]

So would:
[tt]char returnbuf[2023] = "No Info found!\n" ;
char *prbuf = &returnbuf[0] ;

case SQL_TYP_NINTEGER: /* long with null indicator */
prbuf += sprintf(prbuf, "%ld", *(long *) ptr) ;
printf("buffer: %s\n", returnbuf) ;
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top