Advertisement

String's Again (appending one to another)

Started by August 22, 2001 07:43 AM
5 comments, last by shalrath 23 years, 6 months ago
This code should work I believe, but it returns strings such as "./player/up/0.ra0awr" does anyone know why? const int i = 8; for (i = 0; i < frames; i++, texindex++) CreateAlphaTexture(g_Texture, AppendString("./player/walk/up/", ".raw", "%d", i) , texindex, 94, 94); char *AppendString (char *str1, char *str2, char* mid, ...) { va_list args; char *parms; va_start(args, mid); vsprintf(parms, mid, args); va_end(args); strcat(str1, parms); strcat(str1, str2); ErrorMessage("dude", str1); return str1; }
No, because when you define strings like that, they are fixed blocks of memory and you can''t append to them. In this specific example, you shouldn''t be appending things to the end of "./player/walk/up/". You need to allocate memory to put that string in, and leave enough extra space for whatever you''re appending, too.

This kind of thing is a good reason to learn about standard strings, if you are using C++.
Advertisement
Now I use this:
char *AppendString (char *str1, char *str2, char* mid, ...)
{
va_list args;
char *parms;
char allocated[100]; //No file names, etc. longer than 99 characters plz

va_start(args, mid);
vsprintf(parms, mid, args);
va_end(args);

strcpy(allocated, str1); //Place the first part of the string on!
strcat(allocated, parms); //Add the parameters, and the part in hte middle
strcat(allocated, str2); //Place the extension on (for file names)

ErrorMessage("Dude", allocated);
str1 = (char *)allocated;
return str1; //Return the string
}

But all I get in return from this func is a bunch of ASCII goobledygok, ne1 know what''s wrong???
You are returning a pointer to a *local* variable. When you exit the function, the memory may be filled with anything.

Either make your allocated array static (I''d vote against it myself), dynamically allocate the str1 pointer to hold the content of allocated, or go the C++ way and use std::string to append (that would be my choice).

Also, you''re using strcpy() with a fixed length array. Beware of buffer overflows. If you try to write a string that''s too long your your buffer, you''ll overwrite whatever is next in stack (that could very well be the address of the calling function and lead to disastrous errors). Check the length before copying.
If you're going to do it in C, do it this way:
    char *AppendString (char *str1, char *str2, char* mid, ...){   va_list args;   char parms[MAX_MID_LENGTH];   char *ReturnString;   va_start(args, mid);   vsprintf(parms, mid, args);   va_end(args);   ReturnString = (char *)malloc(strlen(str1) + strlen(str2) +  strlen(parms) + 1);   strcpy(ReturnString, str1);   strcat(ReturnString, parms);   strcat(ReturnString, str2);   return ReturnString;}    


Be sure to free the returned string once you use it and define MAX_MID_LENGTH to what you think the maximum length of the mid string might be.

Nutts

edit: removed extra (


Edited by - BeerNutts on August 23, 2001 5:12:03 PM

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

But if you use Beer''s method, don''t forget that it allocates memory, which you later have to call ''free()'' upon, or you get a memory leak.
Advertisement
If i were doing this, I''d do it like this:

  char *AppendString (char *str1, char *str2, char* mid, ...){   va_list args;   char parms[MAX_MID_LENGTH];   static char ReturnString[MAX_STRING_LENGTH];.   va_start(args, mid);   vsprintf(parms, mid, args);   va_end(args);.   ReturnString[0] = ''\0'';   strcpy(ReturnString, str1);   strcat(ReturnString, parms);   strcat(ReturnString, str2);.   return ReturnString;}  


That way, you can use AppendString() inside an expression, like in the example you gave. This time, though, you have to be careful to only use it once within any given expression. Because it writes to static memory, the next time you call it, it will overwrite the last string.


dHarding.net - Just click it.



This topic is closed to new replies.

Advertisement