lstrcat

Started by
3 comments, last by Vopisk 18 years, 3 months ago
hi, was wondering if anyone is able to help please trying to concatenate multiple strings in directx and it only seems to be displaying the string it is initialised with, this is the code im using
 
LPSTR Total=  bob[0]->suit;

for (int i = 0; i < 10; i++)
{
lstrcat(Total, bob->suit);
}

return Total;
	
}
thanks in advance
http://stowelly.co.uk/
Advertisement
In C, strings don't work that simply, unfortunately.

Each string is technically a pointer to the first byte of the string in memory. The string consists of all bytes in memory from that first point until the first null byte (byte with value of 0).

If you're writing code in C++, I highly recommend looking into std::string instead. If you're genuinely aiming for classic C, then here's what your code is doing:

LPSTR Total= bob[0]->suit;
This is creating a pointer to a string called Total, and assigning that pointer to point to the same memory as bob[0]->suit points to. This doesn't make a copy of bob[0]->suit - it just points two string pointers to the same spot in memory.

lstrcat(Total, bob->suit);
This will find the end of the string in Total (the first null byte) and start copying bob->suit into that memory. Total, however, is pointing to bob[0]->suit. The first time this loop runs, it is going to try to copy bob[0]->suit onto itself in memory, which is a bit shaky! Basically, though, the net effect is that bob[0]->suit will probably not be changed.

The next time the loop runs, it will try to append bob[1]->suit onto bob[0]->suit. Depending on how your strings are allocated, chances are this is going to start writing into memory that it shouldn't, and chaos is very likely to happen at this point.


If you need a safe, C-style implementation, here's one that will work. It is far from optimal, but it will get the job done, and hopefully will be fairly clear:

  int i;  LPSTR Total = 0;  int NeededMemory = 0;  int CopyLen;  /* Count up the length of all the strings we will concatenate */  for(i = 0; i < 10; ++i)  {    NeededMemory += lstrlen(bob->suit);  }  /* Allocate the memory we need, plus a little extra for the null terminator */  NeededMemory += 2;  Total = malloc(NeededMemory);  /* Make sure the allocation succeeded */  if(!Total)  {     /* Maybe show an error message here */     return 0;  }  /* Set the memory to all nulls.     This is so, just in case anything goes wrong,     the string will always be terminated (since     we technically have an extra byte at the end     more than we really need). This is good safe     practice when working with C-style strings. */  memset(Total, 0, NeededMemory);  /* Now do the copy */  for(i = 0; i < 10; ++i)  {    lstrcat(Total, bob->suit);  }  /* Return the filled string - make sure to call free() on it later! */  return Total;


This will request the needed amount of memory to make sure you have space to hold the final string. It's important that at some point you free() that pointer, or you will "leak memory." This is part of the danger of using C-style strings, and part of why, if C++ is the actual language you're working in, it's best to prefer to use C++ strings instead.

Hope that clears things up a bit [smile]

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

thanks for that, will have a look at std:string and your example, cheers
http://stowelly.co.uk/
excellent, that was so much easier than the way i was doing it thanks
string Total; for (int i = 0; i < 10; i++) {	 Total = Total +  bob->suit; }  return Total;
http://stowelly.co.uk/
Quote:Original post by Stowelly
excellent, that was so much easier than the way i was doing it thanks



string Total;

for (int i = 0; i < 10; i++)

{

Total = Total + bob->suit;

}

return Total;


To make it even easier, and more proper...

string Total;  for (int i = 0; i < 10; i++)    {      Total += bob->suit;    }  return Total;


You'll notice the only difference is the use of the (+=) append operator. Just my personal opinion, but it makes things cleaner and makes it more clear that your intention is to add whatever is stored at bob->suit to the end of Total.

This topic is closed to new replies.

Advertisement