473,388 Members | 1,390 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,388 software developers and data experts.

sprintf() problem

6
I encountered the problem as follows:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. char* fun(char* b)
  4. {
  5.   char* sTmp;
  6.   static char d[BUFSIZ];
  7.   bzero(d, BUFSIZ);
  8.   sprintf(d, "%s:%s", "Test", b);
  9.   sTmp = d;
  10.   return  sTmp;
  11. }
  12.  
  13. main()
  14. {
  15.   char a[100];
  16.   char b[100];
  17.   char tmp[100];
  18.   bzero(tmp, 100);
  19.   strcpy(a, "ABC123");
  20.   strcpy(b, "CDE456");
  21.   sprintf(tmp, "a: %s\n", fun(a));
  22.   printf("%s", tmp);
  23.   sprintf(tmp, "b: %s\n", fun(b));
  24.   printf("%s", tmp);
  25.   sprintf(tmp, "a: %s b: %s\n", fun(a), fun(b));
  26.   printf("%s", tmp);
  27. }
  28.  
/* result */

a: Test:ABC123
b: Test:CDE456
a: Test:CDE456 b: Test:CDE456

The final printf returned "CDE456" twice instead of "ABC123" and "CDE456". Could you please help to resolve the problem?

Thank you

Karlc
Mar 19 '07 #1
11 4455
horace1
1,510 Expert 1GB
your function fun() uses a static char d[] to hold the string so when you call
Expand|Select|Wrap|Line Numbers
  1.   sprintf(tmp, "a: %s b: %s\n", fun(a), fun(b));
  2.  
the result of the last fun(a) or fun(b) call is printed twice, i.e. if fun(a) is called first d[] holds "ABC123" when fun(b) is called this overwrites d[] with "CDE456" and hence "CDE456" is printed twice.

note with using g++ I get
a: Test:ABC123
b: Test:CDE456
a: Test:ABC123 b: Test:ABC123

this is because the order of the evalution of the function parameters is not specified, i.e. fun(a)may be called by called before fun(b) or the other way around
Mar 19 '07 #2
karlc
6
Dear horace1

So how could I fix this problem? Please help.

Charles

your function fun() uses a static char d[] to hold the string so when you call
Expand|Select|Wrap|Line Numbers
  1.   sprintf(tmp, "a: %s b: %s\n", fun(a), fun(b));
  2.  
the result of the last fun(a) or fun(b) call is printed twice, i.e. if fun(a) is called first d[] holds "ABC123" when fun(b) is called this overwrites d[] with "CDE456" and hence "CDE456" is printed twice.

note with using g++ I get
a: Test:ABC123
b: Test:CDE456
a: Test:ABC123 b: Test:ABC123

this is because the order of the evalution of the function parameters is not specified, i.e. fun(a)may be called by called before fun(b) or the other way around
Mar 19 '07 #3
karlc
6
Sorry for my mistake. There shouldn't be static keyword.

If fact, the problem still existed when I removed it.

Please help. Thank you.
Mar 19 '07 #4
horace1
1,510 Expert 1GB
Dear horace1

So how could I fix this problem? Please help.

Charles
you could use malloc
http://www.cplusplus.com/malloc

to create a new array each time you call the function
Expand|Select|Wrap|Line Numbers
  1.   char* sTmp=malloc(BUFSIZ);
  2.  
however, remember to free() the allocated memory once you have finished with it
Mar 19 '07 #5
horace1
1,510 Expert 1GB
Sorry for my mistake. There shouldn't be static keyword.

If fact, the problem still existed when I removed it.

Please help. Thank you.
if you don't use static in this context the storage allocated to d[] is lost on exit from the function fun(). The progam MAY still appear to work but can cause segmentation errors when the memory is used for something else later on.
Mar 19 '07 #6
karlc
6
Dear Horace1

malloc() did solve the problem. It is what I expected. Thank you.

However, as you reommended, how could I free() the memory if I need to return sTmp in the fun()? Could I free() the memory outside the fun()?

Please help.

Thank you

Charles
Mar 19 '07 #7
horace1
1,510 Expert 1GB
Dear Horace1

malloc() did solve the problem. It is what I expected. Thank you.

However, as you reommended, how could I free() the memory if I need to return sTmp in the fun()? Could I free() the memory outside the fun()?

Please help.

Thank you

Charles
yes, you just need the pointer
Mar 19 '07 #8
chella
51
Sorry for my mistake. There shouldn't be static keyword.

If fact, the problem still existed when I removed it.

Please help. Thank you.
Hi karlc,
Try executing this code. I hope u'll get the desired output.

#include <stdio.h>
#include<string.h>
void fun(char* b,char *str)
{
char d[100];
sprintf(d, "%s:%s", "Test", b);
strcpy(str,d);
}

int main()
{
char a[100];
char b[100];
char tmp[100];
char *str1,*str2;
str1=(char *)malloc(sizeof(char));
str2=(char *)malloc(sizeof(char));
strcpy(a, "ABC123");
strcpy(b, "CDE456");
fun(a,tmp);
printf("%s\n", tmp);
fun(b,tmp);
printf("%s\n", tmp);
fun(a,str1);
fun(b,str2);
sprintf(tmp, "a: %s b: %s\n", str1, str2);
printf("%s", tmp);
return 0;
}

Regards,
Chella
Mar 19 '07 #9
karlc
6
Dear Horace1

Glad to receive your reply again. You example worked well. Thank for your time.

However, I would like to keep return type of fun() be char*, because the function will be called by macro like:

#define MACRO1 fun("aaa")
#define MACRO2 fun("bbb")
....

I though malloc() could help as you advised. The only problem was how to free the memory in the char* fun()

Shall I declare sTmp as global variable (it seemed not good to declare global variables ...). Do you have any other good suggestions?

BTW, Thank for your help

Karlc

Hi karlc,
Try executing this code. I hope u'll get the desired output.

#include <stdio.h>
#include<string.h>
void fun(char* b,char *str)
{
char d[100];
sprintf(d, "%s:%s", "Test", b);
strcpy(str,d);
}

int main()
{
char a[100];
char b[100];
char tmp[100];
char *str1,*str2;
str1=(char *)malloc(sizeof(char));
str2=(char *)malloc(sizeof(char));
strcpy(a, "ABC123");
strcpy(b, "CDE456");
fun(a,tmp);
printf("%s\n", tmp);
fun(b,tmp);
printf("%s\n", tmp);
fun(a,str1);
fun(b,str2);
sprintf(tmp, "a: %s b: %s\n", str1, str2);
printf("%s", tmp);
return 0;
}

Regards,
Chella
Mar 19 '07 #10
horace1
1,510 Expert 1GB
Dear Horace1

Glad to receive your reply again. You example worked well. Thank for your time.

However, I would like to keep return type of fun() be char*, because the function will be called by macro like:

#define MACRO1 fun("aaa")
#define MACRO2 fun("bbb")
....

I though malloc() could help as you advised. The only problem was how to free the memory in the char* fun()

Shall I declare sTmp as global variable (it seemed not good to declare global variables ...). Do you have any other good suggestions?

BTW, Thank for your help

Karlc
I was assuming you do return a char * function result as in your original code, e.g.
Expand|Select|Wrap|Line Numbers
  1. char* fun(char* b)
  2. {
  3.   char* sTmp=malloc(BUFSIZ);
  4.   bzero(sTmp, BUFSIZ);
  5.   sprintf(sTmp, "%s:%s", "Test", b);
  6.   return  sTmp;
  7. }
  8.  
  9.  
Mar 19 '07 #11
karlc
6
Horace1

Yes. It was what I did as you said.

Is it OK not to call free() in the fun()?

Charles

I was assuming you do return a char * function result as in your original code, e.g.
Expand|Select|Wrap|Line Numbers
  1. char* fun(char* b)
  2. {
  3.   char* sTmp=malloc(BUFSIZ);
  4.   bzero(sTmp, BUFSIZ);
  5.   sprintf(sTmp, "%s:%s", "Test", b);
  6.   return  sTmp;
  7. }
  8.  
  9.  
Mar 19 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Huey | last post by:
Hi All, I saved hashed values of hex into buf, then buf becomes unreadable by the means of calling puts(buf), printf("%s") and so on. Then, I need to convert the hex in buf into a char buffer...
13
by: Yodai | last post by:
Hi all.... I have a little problem that's driving me nuts. I can't seem to make any sense of it. I have this small webserver that substitutes some data from a page when finds a substitution...
2
by: aap | last post by:
I have the following code #define MAX 32 struct A { char carr; int iarr; int i; }; void main() {
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;...
10
by: Saurabh | last post by:
Hi all, I am working on RedHat Linux GCC 3.0. I am trying to convert a long to string through sprintf. but i m getting segmantation fault. I tried snprintf also but no avail.here is the piece...
5
by: tjay | last post by:
Hi. I wrote some code using sprintf and atof to store a double as a string of fixed length and to convert it back to a double variable. The string is stored in a char buffer global variable. I'm...
12
by: Henryk | last post by:
Hey there, I have some problems with the following code snippet on a Virtex-4 PowerPC with a GCC based compiler char chData; sprintf(&chData, "%+05.0f", -0.038f); --I get "-000" ???...
15
by: krister | last post by:
Hello, I'm working in a quite large system that has some limitations. One of those is that I can't use printf() to get an output on a screen. I'm forced to use a special function, let's call it...
3
by: google | last post by:
Consider the following code: char str; char str2; strcpy(str, "%alfa% %beta% d%100%d %gamma% %delta%"); printf("printf: "); printf("1%s2", str); printf("\nsprintf: "); sprintf(str2, "1%s2",...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.