473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

need of a sprintf like function...

Hello All,

To use sprintf function we have to first create a char * and assign
some memory to it or we have to fixed memory sized array.

eg.
char str[512];//or it can be
//char * str =(char *)malloc(512);
int d=100;
float f=2.22;
sprintf(str,"Test of sprintf %d %f"),d,f);
free(str);
if there any library function which can avoid allocating memory to str.
i.e i can write
char * str = NULL;
and directly pass str to that printf like function and the function
will internally handle the allocation of memory and releasing it.

so i can write the code like
eg.
char * str = NULL;
testprintf(str,"Test of sprintf %d%f"),d,f);

Is there any such library function available?

Regards,
Yogesh Joshi

Nov 15 '05 #1
4 2354
yp*********@indiatimes.com wrote:
Hello All,

To use sprintf function we have to first create a char * and assign
some memory to it or we have to fixed memory sized array.

eg.
char str[512];//or it can be
//char * str =(char *)malloc(512);
Don't cast the result of malloc. Make sure you do #include <stdlib.h>
int d=100;
float f=2.22;
sprintf(str,"Test of sprintf %d %f"),d,f);
free(str);
if there any library function which can avoid allocating memory to str.
i.e i can write
char * str = NULL;
and directly pass str to that printf like function and the function
will internally handle the allocation of memory and releasing it.
Sure, you can write a function like that. To do it properly, you'll need
to use vsnprintf inside your function, but vsnprintf was added by C99
and has not yet appeared in all implementations of the C standard library.
so i can write the code like
eg.
char * str = NULL;
testprintf(str,"Test of sprintf %d%f"),d,f);


This exact form is not possible, since testprintf must modify the value
of the pointer str, but it receives only a copy of the value, and has no
way to return the new value to the caller. You need to pass a pointer to
str.
testprintf(&str,"Test of sprintf %d%f", d, f);

The prototype of the function should be
int testprintf(char **pstr, const char *format, ...);

Here's a possible implementation:

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

int testprintf(char **pstr, const char *format, ...)
{
va_list ap1, ap2;
va_start(ap1, format);
va_copy(ap2, ap1);
int n = vsnprintf(NULL, 0, format, ap1);
*pstr = malloc(n);
if(*pstr)
{
vsnprintf(*pstr, n, format, ap2);
}
va_end(ap1);
va_end(ap2);
return n;
}

int main(void)
{
char *str;
testprintf(&str, "test sprintf %d %f", 42, 42.0);
printf("%s\n", str);
free(str);
return 0;
}

--
Simon.
Nov 15 '05 #2
yp*********@indiatimes.com wrote:
if there any library function which can avoid allocating memory to str.
i.e i can write
char * str = NULL;
and directly pass str to that printf like function and the function
will internally handle the allocation of memory and releasing it.


Yes. The C99 function snprintf has a length parameter, which you may
set to 0.

--
Thad

Nov 15 '05 #3
Thad Smith wrote:
yp*********@indiatimes.com wrote:
if there any library function which can avoid allocating memory to str.
i.e i can write
char * str = NULL;
and directly pass str to that printf like function and the function
will internally handle the allocation of memory and releasing it.

Yes. The C99 function snprintf has a length parameter, which you may
set to 0.


Setting the length parameter of snprintf to zero does not actually
accomplish the task of allocating memory. It just allows you determine
what length the string should have. You can then allocate a block of
memory of the correct length, and call snprintf again to actually
perform the conversion to string. See my other post in this thread.

--
Simon.
Nov 15 '05 #4

<yp*********@indiatimes.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hello All,

To use sprintf function we have to first create a char * and assign
some memory to it or we have to fixed memory sized array.

eg.
char str[512];//or it can be
//char * str =(char *)malloc(512);
int d=100;
float f=2.22;
sprintf(str,"Test of sprintf %d %f"),d,f);
free(str);
if there any library function which can avoid allocating memory to str.
i.e i can write
char * str = NULL;
and directly pass str to that printf like function and the function
will internally handle the allocation of memory and releasing it.


Well, if it did that, when it returned, whatever
it wrote to the allocated memory would be gone.
I don't see how that would be useful.

-Mike
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: comp.lang.php | last post by:
Hi. Relative php newbie but longtime coder in other langs. Environment: PHP4. Looking for a language construct or function that gives the opposite of array(), example: function xyz($fmt,...
15
by: Earth | last post by:
Hi all, I am new to c and trying the sprintf function. I have written a testing program to try the sprintf fuction and expect the output is 1.234. However, the output shows nothing. Am I...
6
by: jt | last post by:
I need to produce 1 character array from 3 others. I tried sprintf and it terminates on the first 0, null, 0x00 it sees in tmp data. All 3 args print out nice by themselves. By trying to make...
3
by: portergrouptx | last post by:
I am trying to pad a string with leading character zeros. There seems to be a difference between the behavior of sprintf on Windows (Microsoft Visual C++ .NET) and on MVS. Can anyone explain the...
1
by: jimjim | last post by:
Hello, I was wondering about the implications of giving as an argument to sprintf a different data type from the one specified in the format argument. This type of question along with some...
12
by: babak | last post by:
Hi everyone I want to format a string (using sprintf) and put it in a messagebox but however I try to do it, it doesn't seem to work. Here is an example sample of what i try to do: char msg;...
7
by: nass | last post by:
hi all, i am running slackware linux and need to use some function that will will enable me to write and read from a shared mem segment.. i am using open() , to open a file, and then use mmap to...
6
by: merrittr | last post by:
I am trying to build variables for a function using sprintf. However they don't seem to be proper char strings since submiting literals seems to work fine. Any advice to get me rolling? ...
173
by: Ron Ford | last post by:
I'm looking for a freeware c99 compiler for windows. I had intended to use MS's Visual C++ Express and use its C capability. In the past with my MS products, I've simply needed to make .c the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.