   dynamically build a char* string from an integer and a const char*
  I'm trying to dynamically build a char* string by taking an integer and concatenating another character and ".bmp". Sort of like this:for (int x = 0; x< 4; x++){   for (int i=0; i<13; i++){ int idx = (i+1) % 14 ; char *c = "" ; itoa(idx,c,10); Card* tmp = new Card(); strcat(tmp->fname, c); // fname is a new char() if ( x == 0 )    strcat(tmp->fname, "c.bmp"); if ( x == 1 )    strcat(tmp->fname, "h.bmp"); if ( x == 2 )    strcat(tmp->fname, "d.bmp"); if ( x == 3 )    strcat(tmp->fname, "s.bmp");  theDeck->Add(tmp); // add the card to the deck   }}This code doesn't even make it past itoa(). I get a C0000005 error. I've tried various work arounds without any luck. Could someone help me with this?   
  c needs to be a pointer to an array of characters, not to a constant string. Trychar c[32];itoa(idx, c, 10);The same applies to tmp->fname.If you are using C++ however, you'd be better off using std::string and boost::lexical_cast.   ;  Thank you very much. It worked perfectly. On with the game...   ;Quote:Original post by vinb Card* tmp = new Card();Oh good, we're using C++. Let's really use it please? :)// Where necessary#include <string>#include <sstream>// inside Card declaration// This is a real representation of text data. Forget about char* for now.std::string filename;// inside the loop// The "string stream" is just like a console IO stream, except it writes to/// reads from a string.std::stringstream ss;ss << idx;switch (x) {  case 0: ss << "c.bmp"; break;  case 1: ss << "h.bmp"; break;  case 2: ss << "d.bmp"; break;  case 3: ss << "s.bmp"; break;}// Extract the string from the stringstream and assign it to the filename.tmp->filename = ss.str();theDeck->Add(tmp);You'll notice I translated the if logic into a switch statement - the switch is better organized, and avoids repeating the code for all the equality checks - it says, in just one place, "depending on what x is, do different things" - which is what we want.However - "doing something different" each time through a loop makes little sense. There's still value to looping here (otherwise we have to write the inner loop for the card ranks four times), but what we should be trying to do is the same "thing" each time, just perhaps with different input. In our case, the "thing" to do is appending to the stringstream, and the "input" is the four different string literals (an unfortunate name - these double-quoted thingies are indeed of type const char* const, and are referred to as strings because back in the old days of C that was the only real option you had.)Now it's time to re-remember those char*'s and use them for an acceptable purpose - for holding the constant values. What we will do is make an array of these - so that is of type char*[] - and use the x value as an index into that array. This is why we make loop indices start at 0 most of the time - so they can be used as array indices.By constrast, the 'i' value is only used to calculate 'idx', and some analysis shows that the % operator there never does anything - i has a maximum value of 12, so i+1 has a maximum value of 13 which is still 13 after the modulus.As for that filename member itself, there is no reason we should be giving direct access to it from outside. A Card shouldn't change during its lifetime so we shouldn't need to change its image at all, least of all from outside. What we're really trying to do is specify it for the purpose of initializing the Card - which means it should be a constructor parameter. (We need to rewrite the Card constructor so it accepts an argument of type "const std::string&" - it might look like this:)class Card {  // ...  std::string filename;  // ...  public:  Card(const std::string& filename) : filename(filename) {}}Finally, there is probably no good reason to be allocating the Cards dynamically. Inside the Deck we should have some container (I assume you're using just an array for now) of Card objects rather than Card pointers, because the pointers just add complexity here (which isn't needed: we're not going to subclass Card or share them between Decks).Putting all that together:// Make the array of endings for file names.const char* suitImageNames[] = {  "c.bmp", "h.bmp", "d.bmp", "s.bmp"};// We have x range 0 to 3 inclusive so we can use it as an index.for (int x = 0; x < 4; ++x) {  // We'll have i range 1 to 13 inclusive so we can use it directly  // in the file name.  for (int i = 1; i <= 13; ++i) {    // First, create the name.    std::stringstream ss;    ss << i << suitImageNames[x];    // Then create a Card on the stack, now that we know everything we    // need in order to make it, and add it to the Deck - the Add function    // needs to be rewritten to accept a const Card& instead of Card*.    theDeck->Add(Card(ss.str()));  }};  Your advice is much appreciated. Yours is a much better design. I like to think that my designs are better thought out than this, but I've been getting hung up on little things (like char pointers!). Once I get things close to working, I would like to clean things up and refine the design (hmmm, that sounds a little backwards. oh well.) As for the strings, I thought of using them, but the problem is that I need to cast card->fname to LPCSTR for rendering the image, and I'm pretty sure I can't do that with strings. Am I wrong about that?   ;Quote:Original post by vinbbut the problem is that I need to cast card->fname to LPCSTR for rendering the image, and I'm pretty sure I can't do that with strings. Am I wrong about that?Yes you are. That's what std::string::c_str() is for. (Constness can be casted away for compatibility with legacy APIs.)   ;  That's too funny. Had I asked the right question I could have avoided a lot of pain. Then again, I would have also avoided a lot of learning. I'm not sure if I want to rewrite the whole thing with strings at this point, though. But it's good to know in the future. As it turns out, the game is progressing nicely. I've got another question, but that's another post...   