• Advertisement
Sign in to follow this  

lstrcat

This topic is 4383 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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[i]->suit);
}

return Total;
	
}
thanks in advance

Share this post


Link to post
Share on other sites
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[i]->suit);
This will find the end of the string in Total (the first null byte) and start copying bob[i]->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[i]->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[i]->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]

Share this post


Link to post
Share on other sites
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[i]->suit;
}
return Total;

Share this post


Link to post
Share on other sites
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[i]->suit;

}

return Total;


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


string Total;
for (int i = 0; i < 10; i++)
{
Total += bob[i]->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[i]->suit to the end of Total.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement