whats the differance, copying strings

Started by
4 comments, last by Aardvajk 16 years, 3 months ago
im having some issues with copying strings... im trying to copy a string from a temporary adress to a permanent using the string copy function (CASE2)... but it doesnt work... instead i have to do it like in CASE1, which for some reason works... im sending the string as a shader variable later on... and in CASE1 i get correct results whereas CASE2 is wrong... i dont rly see whats the difference?

// CASE 1
int size = 12;
temp.Name = new char[size+1];
for (int k = 0; k < size; k++)
{
	temp.Name[k] = d3dxEffects.pDefaults[n].pParamName[k];
}
temp.Name[size] = '\0';


//CASE 2
StringCchCopyA(temp.Name, MAX_PATH, d3dxEffects.pDefaults[n].pParamName);  


Advertisement
std::string. 'Nuff said.

Σnigma
Do you allocate temp.Name in CASE 2?
int size = 12+1;temp.Name = new char[size];StringCchCopyA(temp.Name, size, d3dxEffects.pDefaults[n].pParamName);  


But better yet...

struct Whatever { ... std::string Name;};...Whatever temp;...temp.name = d3dxEffects.pDefaults[n].pParamName;...// to pass to shader:foo( temp.name.c_str() );
well the problem is that the source is char* and the dest has to be char*.. would "string" still work?

thx that solved it.. had to use the data() function to get it working
Quote:Original post by Dragon_Strike
thx that solved it.. had to use the data() function to get it working


Be aware that std::string::data() does not necessarily return a null-terminated string, whereas std::string::c_str() does.

Antheus's second example above shows the usage of c_str(). When you want a null terminated C-style string, you should prefer this over data().

This topic is closed to new replies.

Advertisement