Answered by:
nesting strings

Question
-
Hello,
I am using sprintf to create a nested string like this:
char* str1[5000]; char* str2[5000]; int num, limit; . . . sprintf(str1,"%d",num); for (i=0; i<limit; i++) { sprintf(str2, "some text %s some more text", str1) strcpy(str1, str2); }
but, I am getting very weird results, and the final string just looks like it was nested way too many times what I actually wrote
hope for some help,
thanks
EinatMonday, May 18, 2009 7:47 AM
Answers
-
Hi all,
I am not sure why, but after deleting all the .obj and the .exe files and re building,
the code seams to work as expected,
so,thank you all for your help, this problem is now solved.
Einat- Proposed as answer by Joseph w Donahue Friday, May 22, 2009 10:46 PM
- Marked as answer by EinZuk Saturday, May 23, 2009 6:30 AM
Friday, May 22, 2009 3:26 PM
All replies
-
but, I am getting very weird results, and the final string just looks like it was nested way too many times what I actually wrote
sprintf(str1,"%d",num);
for (i=0; i<limit; i++)
{
sprintf(str2, "some text %s some more text", str1)
strcpy(str1, str2);
}
hope for some help,
thanks
Einat
if str1=100 before for loop,
then finally str1 should look like:
i=1:
"100" + "some text 100 some more text"
i=2
"100some text 100 some more text"+ "some text 100some text 100 some more text some more text"
I doubt if you really wanted this. This is what should be the result for me from your loop.
fundoosParadiseMonday, May 18, 2009 10:14 AM -
Use strncpy() or strcpy_s() to avoid buffer overflows
Monday, May 18, 2009 10:54 AM -
Quote>char* str1[5000]; // creates an array of 5000 pointers to type char
Quote>char* str2[5000]; // creates an array of 5000 pointers to type char
Quote>sprintf(str1,"%d",num); // tries to copy a C-style string into an array of pointers
I seriously doubt that this code will actually compile. It should generate an error.
Quote>sprintf(str2, "some text %s some more text", str1)
The same here.
- WayneMonday, May 18, 2009 4:13 PM -
sorry, my mistake, the * don't belong there.
it's suppose to be char str1[5000]Monday, May 18, 2009 8:15 PM -
I also missed the '*'
Could you tell us what were your expectations regarding the string obtained in str1[].
"I am getting very weird results" doesn't tell much.Monday, May 18, 2009 9:00 PM -
Hi EinZuk,
As Belloc said, could you tell us what's your weird result? We can't indicate the issue directly based on your information.
Thanks,
Nancy
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Friday, May 22, 2009 10:24 AM -
Hi all,
I am not sure why, but after deleting all the .obj and the .exe files and re building,
the code seams to work as expected,
so,thank you all for your help, this problem is now solved.
Einat- Proposed as answer by Joseph w Donahue Friday, May 22, 2009 10:46 PM
- Marked as answer by EinZuk Saturday, May 23, 2009 6:30 AM
Friday, May 22, 2009 3:26 PM